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

feat: user-defined function that returns a vector instead of a single value #567

Open
guilhem-martin opened this issue Dec 8, 2024 · 4 comments

Comments

@guilhem-martin
Copy link

Hello,

I'd like to define a function like "prime number integer factorization".
Example: decomp(15) would return [3,5] ; decomp(61) would return [61].

As far as I understand, it's not possible for a user-defined function to return several values.
This issue is to suggest an evolution to support return multiple values, a list for instance.

Thanks.

@hanna-kn
Copy link
Contributor

hanna-kn commented Dec 8, 2024

It is possible to return multiple values in i vector, but efficient implementation of factorization in a user-defined function would be difficult.

A function which returns all unique prime factors can be defined as follows.
Subfunction: process(if(\x%\p, 0, \p), \p, primes(\x/2))
Function: if(element(\1, 1)=0, \x, limits(\1, 1, sum(\p>0, 1, elements(\1))))

@guilhem-martin
Copy link
Author

Many thanks for your help!

@hanna-kn
Copy link
Contributor

hanna-kn commented Dec 8, 2024

I just realised that there is a much is easier way to define the prime factors function:
select(primes(\x),\x%\p=0,\p)

@hanna-kn
Copy link
Contributor

hanna-kn commented Dec 9, 2024

Considering that the above function is much less efficient (noticeable for value > 100000) than using "to factors" (e.g. 15 to factors returns 3 × 5), and that you are not the first to ask for it, I will add a prime factors function (in code).

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