# Tensor, the Vector/Matrix/Tensor library has reached v1.0!

With the recent addition of Numbers and some improvements of the documentation, Tensor now has reached version `1.0.0`!

### For people that do not yet know about it:

Tensor is a library that allows you to work with sparse Vectors, Matrices and higher-order Tensors, with the following nice features:

• An implementation of the Access protocol, so you can do `mymatrix[42][3]`.
• It supports the arithmetic functions you would expect from vectors, matrices and tensors. These are implemented using Numbers, which means that they work on any numeric type.
• What is even more, Tensor itself implements Numbersβ Numeric behaviour, which means that anything that does number arithmetic can now do (elementwise) vector/matrix/tensor arithmetic! It also means that you can nest matrices ad infinitum!
• A sparse implementation: Only elements deviating from the default element of a data structure are stored. This means that e.g. a nearly-empty 10000x10000 element matrix takes up only a neglegible amount of memory.
• While you can work with numbers, you can store anything inside: Using it as a representation of a game board (a matrix for chess, or a 3-dimensional tensor for a Rubikβs Cube), for instance, is something that is very possible.
• Functions to rotate, transpose, transform, combine, separate, map over and reduce vectors/matrices/tensors.

Here are some examples from the Readme:

### Vectors

``````iex> vec = Vector.new([1,2,3,4,5])
#Vector-(5)[1, 2, 3, 4, 5]
iex> vec2 = Vector.new(~w{foo bar baz qux})
#Vector-(4)["foo", "bar", "baz", "qux"]
iex> vec2[2]
"baz"
#Vector-(5)[4, 5, 6, 7, 8]
#Vector-(5)[2, 4, 6, 8, 10]
``````

### Matrices

``````
iex> mat = Matrix.new([[1,2,3],[4,5,6],[7,8,9]],3,3)
#Matrix-(3Γ3)
β                          β
β       1,       2,       3β
β       4,       5,       6β
β       7,       8,       9β
β                          β
iex> Matrix.rotate_clockwise(mat)
#Matrix-(3Γ3)
β                          β
β       7,       4,       1β
β       8,       5,       2β
β       9,       6,       3β
β                          β
iex> mat[0]
#Vector-(3)[1, 2, 3]
iex> mat[2][2]
9
iex> Matrix.diag([1,2,3])
#Matrix-(3Γ3)
β                          β
β       1,       0,       0β
β       0,       2,       0β
β       0,       0,       3β
β                          β

#Matrix-(3Γ3)
β                          β
β       3,       4,       5β
β       6,       7,       8β
β       9,      10,      11β
β                          β
#Matrix-(3Γ3)
β                          β
β       2,       4,       6β
β       8,      10,      12β
β      14,      16,      18β
β                          β

``````

### Tensors

``````  iex> tensor = Tensor.new([[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]], [3,3,2])
#Tensor(3Γ3Γ2)
1,       2
3,       4
5,       6
7,       8
9,      10
11,      12
0,       0
0,       0
0,       0
iex> tensor[1]
#Matrix-(3Γ2)
β                 β
β       7,       8β
β       9,      10β
β      11,      12β
β                 β
``````

You can find it on Hex.pm and on GitHub.

11 Likes

Awesome!

1 Like

Great work! I will have to dust off my chess program and use this library to simplify a lot of the functions.

2 Likes

Awesome!

1 Like

This is so cool !

1 Like

Very nice! Reached 1.0.0, been waiting for Numbers. ^.^

1 Like

Iβm using Tensor in one of the Advent of Code solutions and got tripped up by βheightβ vs. βwidthβ.

Itβs been a long time since discrete math, but a search confirms what I thought I remembered: βA matrix with m rows and n columns is said to have dimension m Γ nβ.

But the signature for Matrix.new is `new(list_of_lists \\ [], width, height, identity \\ 0)`.

This seems to be backwards from the usual representation-- the width is the number of columns, while the height is the number of rows.

Matrix.new(3, 2) does what I expect β 3 rows and 2 columns β so this is really just a docs issue. (I opened a PR to fix it. First one, it looks like! )

-Wendy

3 Likes

Wow, thank you so much for your pull request!

It has been merged and added to the new 1.0.1 version of the library.

1 Like

Youβre welcome! Unfortunately I am now completely stuck on how to update an element in the matrix.

Details here: https://github.com/Qqwy/tensor/issues/3

OR if youβd rather questions went elsewhere, feel free to close that and let me know where to ask!

-Wendy

3 Likes