Qqwy

Qqwy

TypeCheck Core Team

I am working on the Tensor library. This adds three modules to your project: Vector, Matrix and Tensor.

As you may or may not know, a Vector is just a one-dimensional Tensor, and a Matrix is a two-dimensional Tensor. Therefore, the Vector and Matrix modules contain some extra functions that only make sense when working with these low-dimensional structures(from_list only makes sense for Vectors, rotate_clockwise only makes sense for Matrices, etc.), but besides that, they delegate many functions to the Tensor module.

Right now, there is only one Struct being manipulated by all three modules: %Tensor{dimensions: [], contents: %{}, identity: 0}. A Vector can be pattern-matched as follows: vector = %Tensor{dimensions: [length]}. A Matrix can be pattern-matched as follows: matrix = %Tensor{dimensions: [height, width]}.

Needless to say, this kind of pattern-matching takes a bit of effort to grasp, and forces people using the library to know about the internal structure of the Vector and Matrix data structures they create.

Because of the impossibility of looking inside a map during guard-clauses, and on the other hand the impossibility to write macros that manage pattern-matching, we find ourselves an interesting Catch-22. This makes things like is_vector() or is_matrix() impossible.

An alternate approach that is possible is to pattern-match a vector or matrix as %Vector{} or %Matrix{}. This looks like the principle of least surprise to me.

The drawback of this second approach, is that it is no longer possible to pattern match on any tensor; in that case, indirection such as the following is required:

def some_function(tensor = %tensor_module{}) when is_tensor_module(tensor_module) do
  ...
end

defmacro is_tensor_module(module) do
  quote do
    unquote(module) in [Vector, Matrix, Tensor]
  end
end

which looks a bit clumsy, and at least also harder to understand to me. All functions inside the library (and all functions outside of it that are supposed to work on any order Tensor) would need this kind of pattern-match/guard macro combo.

So, now I am wondering: What is the better approach here?

  • Keep a single %Tensor{} struct, and accept that people have a higher learning- and pattern-matching curve.

or:

  • Have %Vector{}, %Matrix{} and %Tensor{} as three structs(that are the same, except in name) and modify all functions in the library to accept any of them using above-mentioned guard-macro.

Showing Posts 1 to 1

andre1sk

andre1sk

what if you have %Tensor{type: :vector}

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews