Replies: 4 comments 12 replies
-
|
Interesting idea. Some first thoughts:
|
Beta Was this translation helpful? Give feedback.
-
|
To chime in along the lines of Jos's first comment, I am concerned about an example like My takeaways:
Thanks for considering. |
Beta Was this translation helpful? Give feedback.
-
|
Hi Glen, thanks for your consideration. In your example awkward throws an error import awkward as ak
ak.Array([[1, 2], [3, 4]]) + ak.Array([[1], [3, 4]])
# ValueError: cannot broadcast nested listThis is my misunderstanding. I think the package is more inline with your description, as replacing the portion of extend 1 to a scalar does run a result, broadcasting the scalar (not filling the blanks) import awkward as ak
ak.Array([[1, 2], [3, 4]]) + ak.Array([1, [3, 4]])
# [[2, 3],
# [6, 8]]So I think Jos's first comment, your latest comment and the awkward array package have a shared idea that these operations would recursively apply broadcasting rules but only to broadcast scalars. A downside that I've just found out is that if the arrays are rectangular it doesn't broadcast due to the same limitation. import awkward as ak
ak.Array([[1, 2], [3, 4]]) + ak.Array([[1], [2]])
# ValueError: cannot broadcast nested listBut it would simply work for numpy arrays (rectangular) import numpy as np
np.array([[1, 2], [3, 4]]) + np.array([[1], [2]])
# array([[2, 3],
# [5, 6]])It's a limitation that to be honest is not clear to me, maybe there is a mathematical basis for this. The proposed algorithm would be a superset as it would work the same for rectangular arrays and jagged heterogeneous arrays without knowing beforehand. At the moment I haven't started with the proof of concept in the codebase but I think it could work either way:
Something along those lines. AppendixAnother mistake I made is that I was saying that a = [[1,2], 3]
a + [[1],[2]]
# [[2, 3], 5]But I should have said either: a = [[1,2], 3]
a + [[1],[2]]
# [[2, 3], [5]]or a = [[1,2], 3]
a + [[1], 2]
# [[2, 3], 5] |
Beta Was this translation helpful? Give feedback.
-
|
One other important point: if there is a PR along these lines, it should establish the behavior for jagged arrays with empty sections, even for functions like map and filter and foreach that currently have some behavior on jagged arrays. The submitter of PR #3567 found for example that [[], [1]] and [[1],[]] behave differently in such contexts, which should be corrected. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently mathjs has support for mapping jagged arrays (non rectangular arrays).
config({matrix:"Array"}) a = [[1,2], 3] map(a, _(x) = x+1) # [[2, 3], 4]But operations are not possible,
The broadcasting algorithm could be tweaked to allow jagged arrays, something like this.
config({matrix:"Array"}) a = [[1,2], 3] a + 1 # [[2, 3], 4] a + [[1],[2]] # [[2, 3], 5]Would this functionality be of interest?
Reference:
https://awkward-array.org/doc/main/index.html
Beta Was this translation helpful? Give feedback.
All reactions