al2o3cr

al2o3cr

Calculating element-to-element differences and sums in Nx

Many years ago, I used a system called Yorick for doing array calculations. One feature that was really useful was a series of functions for taking differences and sums within a vector. Ref: Yorick User Manual

The simplest operation to explain is “difference”; when applied to a dimension, it produces a result that’s one element shorter and has successive differences.

[0, 1, 4, 9, 16, 25] (length 6)

has differences

[1, 3, 5, 7, 9] (length 5)

This can be really useful for calculus-y things like “given a list of x values and a list of corresponding y values, estimate the slope as a function of x”.


The almost-inverse of “difference” is “cumulative sum”; when applied to a dimension, it produces a result that’s one element longer and has a running total:

[1, 3, 5, 7, 9] (length 5)

has cumulative sums

[0, 1, 4, 9, 16, 25] (length 6)

It’s an “almost” inverse because the first element of the cumulative sum is always zero. If this reminds you of “plus C” in integrals, you’re not wrong :stuck_out_tongue:


My root question: what’s the best way to write these kinds of calculations in Nx?

It’s certainly possible to produce tensors (via Nx.make_diagonal etc) that can be multiplied by the input vector to produce these results, but it seems inefficient given the simple nature of the calculations.

First Post!

polvalente

polvalente

Nx Core Team

There is Nx.diff and Nx.cumulative_sum for what you want

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement