stevensonmt

stevensonmt

Matrix operation for "shoelace formula"

I’m not terribly familiar with matrix calculations but I’d like to start using Nx to see if I can speed up some things. This question is not directly about Nx so much as generally about the appropriate term for this type of calculation in matrix lingo. To whit:

input = [{x1, y1}, {x2, y2}, {x3, y3}]

matrix = | x1  x2  x3 |
         | y1  y2  y3 |

output = x1 * y2 + x2 * y3 + x3 * y1 - x2 * y1 - x3 * y2 - x1 * y3

Is there a name for that matrix operation?

Marked As Solved

shanesveller

shanesveller

I’ve been recently goofing around with Nx a bunch, including for AOC solutions, and I don’t claim to have a good handle on it, but I’ve been nerd sniped. This is what I tried in a Livebook. Sample inputs were based on the Rosetta Code link from upthread.

Shorter summary:

  • Get to an {2, N} tensor via Nx.transpose
  • Extend it along the axis with Nx.tile, compare with List.duplicate
  • Slice along the same axis to get cells that are offset by one step from the original pairs
  • Stack them a few times so that the tensor contains [[first row, offset second row], [second row, offset first]]
    • I didn’t quickly find out a good way to do this without intermediate variables and Access syntax, but it’s probably quite possible for someone more deft
  • Perform pair-wise multiplication along a single axis to get the x1 * y2... bits
  • Negate the second row of one axis to subtract
  • Sum, absolute value, divide by 2, wrapped in a dbg to see the shorter steps

Very happy to receive pointers and critique. I’m almost certain some of my steps could be elided, beyond just the places where I left intermediate steps to illuminate how the data was shifting in the Markdown output. Also notably I’ll bet this is very not-optimal for allocations.

Pretty much what I arrived at!

Also Liked

gregvaughn

gregvaughn

That looks closely related to a determinant, but those are for square matrices, so I’m not sure what the generalized name might be.

shanesveller

shanesveller

If you’re fine with doing the transform inside the vanilla Elixir surroundings, it’s tough to beat Enum.slide:

Enum.slide(1..5, 0, -1)
[2, 3, 4, 5, 1]

Enum.slide(1..5, 0..1, -1)
[3, 4, 5, 1, 2]

Enum.slide(1..5, 2..3, 0)
[3, 4, 1, 2, 5]

Last Post!

stevensonmt

stevensonmt

Nice. I missed when that got introduced in 1.13.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement