mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

`gcd()` should accept array

Open mattvague opened this issue 3 years ago • 6 comments

I would expect math.gcd([1,2]) to work the same as math.gcd(1,2) however it currently throws "Too few arguments in function gcd (expected: number or BigNumber or Array or Matrix or Fraction or string or boolean, index: 1)"

mattvague avatar May 20 '22 15:05 mattvague

Yes I can imagine that. There are functions like sum and prod that accept both notations, but so far we only have that for functions that have a variable number of arguments, and not for functions that have a fixed number of 1 or 2 arguments (like gcd). I'm not sure if math.gcd([1,2]) is a signature that we should add, I'm not sure if it really adds value. Do you have use cases for it?

josdejong avatar May 20 '22 17:05 josdejong

for functions that have a fixed number of 1 or 2 arguments (like gcd).

gcd does seem to accept a variable number of args though

image

If other fns do it it would be nice to be consistent

mattvague avatar May 20 '22 18:05 mattvague

Do you have use cases for it?

I don't at the moment, I just noticed it didn't work while working on the chain types. However, I do think that it's best to be as forgiving about what kind of arguments are accepted as possible so that you get that "things just work" feeling

mattvague avatar May 20 '22 18:05 mattvague

Ahh, wait a second. I forgot that gcd also supports not only two but an variable number of arguments. Yes makes sense to me to add Array/Matrix support for gcd and lcm because of that.

I'm still a bit in doubt on whether it makes sense to add array support to say xgcd(a, b) which does accept only two arguments.

josdejong avatar May 23 '22 16:05 josdejong

Ahh, wait a second. I forgot that gcd also supports not only two but an variable number of arguments. Yes makes sense to me to add Array/Matrix support for gcd and lcm because of that.

👍🏻

I'm still a bit in doubt on whether it makes sense to add array support to say xgcd(a, b) which does accept only two arguments.

Yes, I agree that if a function only takes 2 args then array support probably wouldn't make sense

mattvague avatar May 23 '22 19:05 mattvague

👌 then we have a clear plan: functions with a variable number of arguments should support array, functions with a fixed number of variables not.

josdejong avatar May 24 '22 07:05 josdejong

@MostafaEwis are still working on that? If not, then I'd like to take this as my first contribution here 🚀 cc: @josdejong

jakubriegel avatar Jan 08 '23 20:01 jakubriegel

@jakubriegel Hi Jakub. No, I'm no longer working on this issue, good luck in fixing it.

MostafaEwis avatar Jan 08 '23 20:01 MostafaEwis

Support for Array is published now in v11.7.0

josdejong avatar Mar 15 '23 10:03 josdejong