Qqwy

Qqwy

TypeCheck Core Team

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"
iex> Vector.add(vec, 3)
#Vector-(5)[4, 5, 6, 7, 8]
iex> Vector.add(vec, vec)
#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│
└                          ┘

iex> Matrix.add(mat, 2)
#Matrix-(3×3)
┌                          ┐
│       3,       4,       5│
│       6,       7,       8│
│       9,      10,      11│
└                          ┘
iex> Matrix.add(mat, mat)
#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.

Most Liked

wsmoak

wsmoak

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! :slight_smile: )

-Wendy

wsmoak

wsmoak

You’re welcome! Unfortunately I am now completely stuck on how to update an element in the matrix. :confused:

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

uranther

uranther

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

vic

vic

Asdf Core Team

Awesome! :heart:

loongmxbt

loongmxbt

Awesome!

Where Next?

Popular in Libraries Top

Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
pkrawat1
Presenting Aviacommerce, open source e-commerce platform in Elixir Aviacommerce is an open source e-commerce platform in Elixir. We at...
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
Qqwy
Solution is a library to help you with working with ok/error-tuples in case and with-expressions by exposing special matching macros, as ...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement