Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping of pyttb ttb functionality #291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
feefeb9
Created ttensor/reconstruct tutorial in master.
jeremy-myers Sep 19, 2023
afd927f
Merge with upstream
jeremy-myers Sep 21, 2023
5b3f607
autoformat
jeremy-myers Sep 21, 2023
05a37a1
Removed from main until completed in branch 248-tutorial-partial-reco…
jeremy-myers Sep 21, 2023
b115908
Merge with upstream
jeremy-myers Sep 26, 2023
2dceaff
Merge with upstream
jeremy-myers Sep 27, 2023
3311b11
Merge with upstream.
jeremy-myers Sep 27, 2023
982a6cd
Merge branch 'main' of https://github.com/sandialabs/pyttb
jeremy-myers Sep 28, 2023
b310115
Initial commit
jeremy-myers Sep 28, 2023
38ec12b
Basic matlab to pyttb mapping. TODOs: 1. insert hyperlinks to both tt…
jeremy-myers Sep 28, 2023
f3babc9
Merge branch 'main' of https://github.com/jeremy-myers/pyttb
jeremy-myers Sep 29, 2023
ccef927
nbstripout
jeremy-myers Oct 2, 2023
19680f2
Merge branch 'sandialabs:main' into main
Oct 3, 2023
8d1c32f
Merge branch 'sandialabs:main' into main
Oct 24, 2023
af30fe5
Merge branch 'main' into mapping-of-pyttb-ttb-functionality
jeremy-myers Oct 24, 2023
16a9f01
Merge branch 'sandialabs:main' into mapping-of-pyttb-ttb-functionality
Dec 11, 2023
a7b595f
Initial version ready.
jeremy-myers Dec 12, 2023
75c0f1b
Updates to ruff
jeremy-myers Dec 13, 2023
ea3613e
Updates to ruff
jeremy-myers Dec 13, 2023
ebd328a
Merge branch 'mapping-of-pyttb-ttb-functionality' of https://github.c…
jeremy-myers Feb 5, 2024
7b71366
Ruff fixes.
jeremy-myers Feb 5, 2024
960fac8
Ruff fixes.
jeremy-myers Feb 5, 2024
3d0e814
Attempting pre-commit fixes with autoupdate.
jeremy-myers Feb 5, 2024
a65d7cd
Attempting pre-commit fixes with autoupdate: part two, running pre-co…
jeremy-myers Feb 5, 2024
6d300c1
Attempting pre-commit fixes with autoupdate: attempt two, autoupdate …
jeremy-myers Feb 5, 2024
9379112
Merge branch 'mapping-of-pyttb-ttb-functionality' of https://github.c…
jeremy-myers Feb 5, 2024
c42b3ab
Attempting to fix pre-commit issues and noqas
jeremy-myers Feb 5, 2024
4c35c29
Pin ruff, fix precommit
jeremy-myers Feb 5, 2024
a9fe513
Run precommit
jeremy-myers Feb 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 24.1.1
hooks:
- id: black
language_version: python
Expand Down
14 changes: 14 additions & 0 deletions docs/source/for_matlab_users.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pyttb for MATLAB users
======================

Already familiar with MATLAB Tensor Toolbox? To assist transitions from the
Tensor Toolbox for MATLAB to pyttb, this guide documents some key differences.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to mention that the tutorials are similar to the MATLAB ones if they want a hands on approach. I think that's already implied but might not be a bad idea to be explicit.


In the ``pyttb`` calling conventions below we'll use the following notation:
- ``pyttb.<TENSOR_TYPE>``: ``X`` (and ``Y`` for binary operators)
- ``<SCALAR_TYPE>``: ``a`` for scalars.

.. toctree::
:glob:

matlab/*
18 changes: 15 additions & 3 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
:orphan:

Getting Started
***************

In construction
Tutorials
=========

.. toctree::
:maxdepth: 1

tutorials.rst

Coming from MATLAB
==================

.. toctree::
:maxdepth: 1

for_matlab_users.rst
7 changes: 3 additions & 4 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ algorithms for computing low-rank tensor models.
.. _Algorithms: algorithms.html


Tutorials
=========
Getting Started
===============

.. toctree::
:maxdepth: 1

tutorials.rst

getting_started.rst

Python API
================
Expand Down
83 changes: 83 additions & 0 deletions docs/source/matlab/common.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
General key differences
-----------------------

Data members
^^^^^^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``size`` | ``shape`` | ``X.shape`` |
+-----------------+----------------------+------------------------------------------------------------------------+

Methods
^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``and`` | ``logical_and`` | ``X.logical_and(Y)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``disp`` | ``__str__`` | ``X`` |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, for all the names pyttb names that include dunder methods I wonder if it makes sense to use their public equivalents. Technically they are all hidden methods that get exposed via some other hook. For example __str__ and __repr__ both get called under the hood for str(X) and repr(X). For someone totally unaware of python just trying to directly translate from matlab based on this doc I'd really hope they don't end up with a bunch of __str__, __rmul__ calls in their code

+-----------------+----------------------+------------------------------------------------------------------------+
| ``display`` | ``__repr__`` | ``print(X)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``eq`` | ``__eq__`` | ``X == Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``ge`` | ``__ge__`` | ``X >= Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``gt`` | ``__gt__`` | ``X > Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``ldivide`` | ``__truediv__`` | ``X / Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``le`` | ``__le__`` | ``X <= Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``lt`` | ``__lt__`` | ``X < Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``minus`` | ``__sub__`` | ``X - Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``mldivide`` | ``__truediv__`` | ``X / Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``mrdivide`` | ``__rtruediv__`` | ``X / Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``mtimes`` | ``__rmul__`` | ``a * X`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``ne`` | ``__ne__`` | ``X != Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``not`` | ``logical_not`` | ``X.logical_not(Y)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``or`` | ``logical_or`` | ``X.logical_or(Y)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``plus`` | ``__add__`` | ``X + Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``power`` | ``__pow__`` | ``X ** a`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``rdivide`` | ``__rtruediv__`` | ``X / Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``subsasgn`` | ``__setitem__`` | ``X[index] = a`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``subsref`` | ``__getitem__`` | ``X[index]`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``tenfun`` | ``tt_tenfun`` | e.g., ``pyttb.tt_tenfun(lambda x: x + 1, A)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``times`` | ``__mul__`` | ``X * Y`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``uminus`` | ``__neg__`` | ``-X`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``uplus`` | ``__pos__`` | ``+X`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``xor`` | ``logical_xor`` | ``X.logical_xor(Y)`` |
+-----------------+----------------------+------------------------------------------------------------------------+

Copying
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to note this is fairly common for non-builtin types in python and include a link. Hereish? https://docs.python.org/3/library/copy.html

^^^^^^^^^^^^^^^^^^^^
Copying a ``pyttb`` tensor works differently than MATLAB. For example in MATLAB, copying a tensor ``Y``
as ``Y = X`` returns a tensor ``Y`` that is independent of ``X``. Changing the value of ``Y`` does not
change the value of ``X``. However, the same syntax in ``pyttb``, ``Y = X``, returns a *shallow copy* of ``X``;
the shallow copy ``Y`` is a *reference* to ``X``. For that reason, each ``pyttb`` tensor class provides a ``copy()``
method that returns a *deep copy* ``Y`` that is independent of ``X``, which is called as ``Y = X.copy()``.

MATLAB methods not included in ``pyttb``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- ``datadisp``
- ``isscalar``
- ``transpose``
- ``tocell``
28 changes: 28 additions & 0 deletions docs/source/matlab/ktensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
``ktensor``
-----------------

Data members
^^^^^^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``lambda`` | ``weights`` | ``X.weights`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``U`` | ``factor_matrices`` | ``X.factor_matrices`` |
+-----------------+----------------------+------------------------------------------------------------------------+

Methods
^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| | ``from_vector`` | ``ttb.ktensor.from_vector(data[:], shape)`` |
| ``ktensor`` +----------------------+------------------------------------------------------------------------+
| | ``from_function`` | ``ttb.tensor.from_function(<function>, shape)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` |
+-----------------+----------------------+------------------------------------------------------------------------+

MATLAB methods not included in ``pyttb``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ``viz``
30 changes: 30 additions & 0 deletions docs/source/matlab/sptenmat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
``sptenmat``
------------

Data members
^^^^^^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``size`` | ``shape`` | ``X.shape`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``tsize`` | ``tshape`` | ``X.tshape`` |
+-----------------+----------------------+------------------------------------------------------------------------+

Methods
^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| | ``from_data`` | ``A = ttb.sptenmat.from_data(subs, vals, rdims, cdims, tshape)`` |
| +----------------------+------------------------------------------------------------------------+
| ``sptenmat`` | ``from_tensor_type`` | ``A = ttb.sptenmat.from_tensor_type(X, np.array([0]))`` |
| +----------------------+------------------------------------------------------------------------+
| | ``from_array`` | ``A = ttb.sptenmat.from_data(B, rdims, cdims, tshape)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``sptensor`` | ``to_sptensor`` | ``X.to_sptensor()`` |
+-----------------+----------------------+------------------------------------------------------------------------+

MATLAB methods not included in ``pyttb``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ``aatx``
14 changes: 14 additions & 0 deletions docs/source/matlab/sptensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
``sptensor``
----------------

Methods
^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| | ``from_function`` | ``ttb.sptensor.from_function(<function>, shape)`` |
| ``sptensor`` +----------------------+------------------------------------------------------------------------+
| | ``from_aggregator`` | ``ttb.sptensor.from_aggregator(subs, vals, shape, function_handle)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` |
+-----------------+----------------------+------------------------------------------------------------------------+
10 changes: 10 additions & 0 deletions docs/source/matlab/sumtensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
``sumtensor``
-------------------

Data members
^^^^^^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``part`` | ``parts`` | ``X.parts`` |
+-----------------+----------------------+------------------------------------------------------------------------+
8 changes: 8 additions & 0 deletions docs/source/matlab/symktensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
``symktensor``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO I think it might be clearer to just remove the placeholders. I'm not sure what the plan is for actually covering these other classes.

--------------------

Data members
^^^^^^^^^^^^

Methods
^^^^^^^
8 changes: 8 additions & 0 deletions docs/source/matlab/symtensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
``symtensor``
-------------------

Data members
^^^^^^^^^^^^

Methods
^^^^^^^
22 changes: 22 additions & 0 deletions docs/source/matlab/tenmat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
``tenmat``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a PR up now that updates the tenmat constructors. I believe this is correct currently but once Danny merges my PR this will go stale.

----------------

Data members
^^^^^^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``tsize`` | ``tshape`` | ``X.tshape`` |
+-----------------+----------------------+------------------------------------------------------------------------+

Methods
^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| | ``from_data`` | ``B = ttb.tenmat.from_data(A.data, A.rindices, A.cindices, A.tshape)`` |
| ``tenmat`` +----------------------+------------------------------------------------------------------------+
| | ``from_tensor_type`` | ``A = ttb.tenmat.from_tensor_type(X, np.array([1]))`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` |
+-----------------+----------------------+------------------------------------------------------------------------+
12 changes: 12 additions & 0 deletions docs/source/matlab/tensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
``tensor``
------------------

Methods
^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``tensor`` | ``from_function`` | ``ttb.tensor.from_function(<function>, shape)`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``sptensor`` | ``to_sptensor`` | ``X.to_sptensor()`` |
+-----------------+----------------------+------------------------------------------------------------------------+
21 changes: 21 additions & 0 deletions docs/source/matlab/ttensor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
``ttensor``
-----------------

Data members
^^^^^^^^^^^^
+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``U`` | ``factors`` | ``X.factors`` |
+-----------------+----------------------+------------------------------------------------------------------------+

Methods
^^^^^^^

+-----------------+----------------------+------------------------------------------------------------------------+
| MATLAB name | ``pyttb`` name | Calling convention |
+=================+======================+========================================================================+
| ``size`` | ``shape`` | ``X.shape()`` |
+-----------------+----------------------+------------------------------------------------------------------------+
| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` |
+-----------------+----------------------+------------------------------------------------------------------------+
Loading
Loading