Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for vectors in math functions #3987

Open
GloinTheDark opened this issue Nov 27, 2021 · 1 comment
Open

Add support for vectors in math functions #3987

GloinTheDark opened this issue Nov 27, 2021 · 1 comment

Comments

@GloinTheDark
Copy link

GloinTheDark commented Nov 27, 2021

min() and max() should work with vectors

ie:

function min(a, b, ... n) = if( 
    /* argument count > 1 and arguments are vectors */
)
[
    for(i = [0: len(a)-1])
        min(a[i], b[i], ... n[i])
]
else
/* default behaviour */;
a = [1, 2, 3];
b = [3, 2, 1];
c = [0, 10, 0];
echo(min(a, b, c));
echo(max(a, b, c));

Output:

ECHO: [0, 2, 0]
ECHO: [3, 10, 3]

Of course it would be recursive.

echo(max(
[
    [1, 5], [6, 2]
], 
[
    [7, 3], [4, 8]
]
));

Output: ECHO: [[7, 5], [6, 8]]

Other math functions when given a vector should return a vector of the element-wise results

Given any math function "f" when the input is a vector:

function f(v) = [
    for(i = v) f(i)
];

floor([1.9, [2.1, -0.5]]) should return [1, [2, -1]]
This would apply to math function that currently take a single scaler argument and return a scaler.
ie: abs, sign, sin, cos, tan, acos, asin, atan, floor, round, ceil, ln, log, sqrt, exp.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@thehans
Copy link
Member

thehans commented Dec 1, 2021

I think it could be useful to accept vectors for builtin math functions (GLSL for example acts similarly for many of its math functions), but I'm not sure about the recursive aspect.

I especially don't feel that min/max should be recursive, since it is dependent on comparison operator, which is already recursive over vectors, ordered lexicographically. With that in mind, if min/max were to accept multiple vectors, i would expect the result to be a single one of those vectors, rather than element-wise comparison.
eg: max([1,3,5],[3,2,1]) == [3,2,1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants