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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nick/tenmat docs #294

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Update tutorials to match new constructors.
  • Loading branch information
ntjohnson1 committed Dec 13, 2023
commit d16efd7172255d5556a7f875c120f11f7a4ac4ea
36 changes: 12 additions & 24 deletions docs/source/tutorial/class_sptenmat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(X, np.array([0])) # Mode-0 matricization\n",
"A = X.to_sptenmat(np.array([0])) # Mode-0 matricization\n",
"A"
]
},
Expand All @@ -81,7 +81,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(X, np.array([1, 2])) # Multiple modes mapped to rows.\n",
"A = X.to_sptenmat(np.array([1, 2])) # Multiple modes mapped to rows.\n",
"A"
]
},
Expand All @@ -92,9 +92,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(\n",
" X, cdims=np.array([1, 2])\n",
") # Specify column dimensions.\n",
"A = X.to_sptenmat(cdims=np.array([1, 2])) # Specify column dimensions.\n",
"A"
]
},
Expand All @@ -105,9 +103,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(\n",
" X, np.arange(4)\n",
") # All modes mapped to rows, i.e., vectorize.\n",
"A = X.to_sptenmat(np.arange(4)) # All modes mapped to rows, i.e., vectorize.\n",
"A"
]
},
Expand All @@ -118,9 +114,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(\n",
" X, np.array([1])\n",
") # By default, columns are ordered as [0, 2, 3]\n",
"A = X.to_sptenmat(np.array([1])) # By default, columns are ordered as [0, 2, 3]\n",
"A"
]
},
Expand All @@ -131,9 +125,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(\n",
" X, np.array([1]), np.array([3, 0, 2])\n",
") # Specify explicit ordering\n",
"A = X.to_sptenmat(np.array([1]), np.array([3, 0, 2])) # Specify explicit ordering\n",
"A"
]
},
Expand All @@ -144,9 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"fc\"\n",
") # Forward cyclic column ordering\n",
"A = X.to_sptenmat(np.array([1]), cdims_cyclic=\"fc\") # Forward cyclic column ordering\n",
"A"
]
},
Expand All @@ -157,9 +147,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic column ordering\n",
"A = X.to_sptenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic column ordering\n",
"A"
]
},
Expand Down Expand Up @@ -236,7 +224,7 @@
"metadata": {},
"outputs": [],
"source": [
"B = ttb.sptenmat.from_data(\n",
"B = ttb.sptenmat(\n",
" A.subs, A.vals, A.rdims, A.cdims, A.tshape\n",
") # Effectively copies A\n",
"B"
Expand All @@ -257,7 +245,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.sptenmat.from_data(\n",
"A = ttb.sptenmat(\n",
" rdims=A.rdims, cdims=A.cdims, tshape=A.tshape\n",
") # An empty sptenmat\n",
"A"
Expand Down Expand Up @@ -298,7 +286,7 @@
"outputs": [],
"source": [
"X = ttb.sptenrand((10, 10, 10, 10), nonzeros=10) # Create sptensor\n",
"A = ttb.sptenmat.from_tensor_type(X, np.array([0])) # Convert to an sptenmat\n",
"A = X.to_sptenmat(np.array([0])) # Convert to an sptenmat\n",
"A"
]
},
Expand Down Expand Up @@ -328,7 +316,7 @@
"metadata": {},
"outputs": [],
"source": [
"B = ttb.sptenmat.from_tensor_type(ttb.sptenrand((3, 3, 3), nonzeros=3), np.array([0]))\n",
"B = ttb.sptenrand((3, 3, 3), nonzeros=3).to_sptenmat(np.array([0]))\n",
"B"
]
},
Expand Down
70 changes: 20 additions & 50 deletions docs/source/tutorial/class_tenmat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"outputs": [],
"source": [
"# Dims [0,1] map to rows, [2,3] to columns.\n",
"A = ttb.tenmat.from_tensor_type(X, np.array([0, 1]), np.array([2, 3]))\n",
"A = X.to_tenmat(np.array([0, 1]), np.array([2, 3]))\n",
"A"
]
},
Expand All @@ -63,7 +63,7 @@
"metadata": {},
"outputs": [],
"source": [
"B = ttb.tenmat.from_tensor_type(X, np.array([1, 0]), np.array([2, 3])) # Order matters!\n",
"B = X.to_tenmat(np.array([1, 0]), np.array([2, 3])) # Order matters!\n",
"B"
]
},
Expand All @@ -73,7 +73,7 @@
"metadata": {},
"outputs": [],
"source": [
"C = ttb.tenmat.from_tensor_type(X, np.array([0, 1]), np.array([3, 2]))\n",
"C = X.to_tenmat(np.array([0, 1]), np.array([3, 2]))\n",
"C"
]
},
Expand All @@ -92,9 +92,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1])\n",
") # np.array([1]) passed to the `rdims` parameter\n",
"A = X.to_tenmat(np.array([1])) # np.array([1]) passed to the `rdims` parameter\n",
"A"
]
},
Expand All @@ -114,7 +112,7 @@
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"# Same as A = ttb.tenmat.from_tensor_type(X, np.array([0,3]), np.array([1,2]))\n",
"A = ttb.tenmat.from_tensor_type(X, cdims=np.array([1, 2]))\n",
"A = X.to_tenmat(cdims=np.array([1, 2]))\n",
"A"
]
},
Expand All @@ -132,9 +130,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, cdims=np.arange(0, 4)\n",
") # Map all the dimensions to the columns\n",
"A = X.to_tenmat(cdims=np.arange(0, 4)) # Map all the dimensions to the columns\n",
"A"
]
},
Expand All @@ -153,9 +149,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([2])\n",
") # By default, columns are ordered as [0, 1, 3].\n",
"A = X.to_tenmat(np.array([2])) # By default, columns are ordered as [0, 1, 3].\n",
"A"
]
},
Expand All @@ -166,9 +160,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), np.array([2, 0, 3])\n",
") # Explicit specification.\n",
"A = X.to_tenmat(np.array([1]), np.array([2, 0, 3])) # Explicit specification.\n",
"A"
]
},
Expand All @@ -178,9 +170,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"fc\"\n",
") # Forward cyclic, [2,3,0].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"fc\") # Forward cyclic, [2,3,0].\n",
"A"
]
},
Expand All @@ -190,9 +180,7 @@
"metadata": {},
"outputs": [],
"source": [
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A"
]
},
Expand All @@ -210,9 +198,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A.data # The 2D numpy array itself."
]
},
Expand Down Expand Up @@ -257,10 +243,8 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"B = ttb.tenmat.from_data(A.data, A.rindices, A.cindices, A.tshape)\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"B = ttb.tenmat(A.data, A.rindices, A.cindices, A.tshape)\n",
"B # Recreates A."
]
},
Expand Down Expand Up @@ -295,9 +279,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A.double() # Converts A to a standard 2D numpy array."
]
},
Expand All @@ -315,9 +297,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"Y = A.to_tensor()\n",
"Y"
]
Expand All @@ -336,9 +316,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A.shape # 2D numpy array shape."
]
},
Expand All @@ -365,9 +343,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A[1, 0] # Returns the (1,0) element of the 2D numpy array"
]
},
Expand All @@ -385,9 +361,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A[0:2, 0:2] = np.ones((2, 2))\n",
"A"
]
Expand Down Expand Up @@ -422,9 +396,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"A.norm() # Norm of the 2D numpy array."
]
},
Expand Down Expand Up @@ -488,9 +460,7 @@
"outputs": [],
"source": [
"X = ttb.tensor(np.arange(1, 25), shape=(3, 2, 2, 2)) # Create a tensor.\n",
"A = ttb.tenmat.from_tensor_type(\n",
" X, np.array([1]), cdims_cyclic=\"bc\"\n",
") # Backward cyclic, [0,3,2].\n",
"A = X.to_tenmat(np.array([1]), cdims_cyclic=\"bc\") # Backward cyclic, [0,3,2].\n",
"B = A * A.ctranspose() # Tenmat that is the product of two tenmats.\n",
"B"
]
Expand Down