sharkdp / insect

High precision scientific calculator with support for physical units
https://numbat.dev/
MIT License
3.17k stars 126 forks source link

Variadic functions #396

Closed garywill closed 1 year ago

garywill commented 1 year ago

This calculator is amazing! I was looking for some tool that allow us to easily calculate circult impedance. Frustrated until I meet Insect !

This is how we can calculate parallel two resistors:

 p(r1, r2) = 1 / ((1 / r1) + (1 / r2))

three resistors:

 p(r1, r2, r3) = 1 / ( (1 / r1) + (1 / r2) + (1 / r3) )

I'm wondering can we define a function with infinite number of arguments?

archisman-panigrahi commented 1 year ago

How would you even supply "infinite" number of input parameters in the runtime? What are you trying to achieve?

archisman-panigrahi commented 1 year ago

In case you are trying to use arbitrary (not infinity) number of inputs, then use a language that supports data structures (e.g. Octave/Python/Julia), instead of insect.

Then you can define an array R = [r1,r2,r3,.....]. and write a function which will accept the array R as an argument, and return the reciprocal of the sum of reciprocals of all the elements in R.

sharkdp commented 1 year ago

Right now, there is only a limited set of variadic functions (with arbitrary number of arguments) in Insect: maximum, minimum, mean. Those are builtin functions, but there is currently no way to define variadic functions as a user.

We are currently working on a complete rewrite of Insect, and variadic functions (OR support for lists/vectors) are definitely on the roadmap — but not yet implemented.

sharkdp commented 1 year ago

Follow up ticket for Numbat: https://github.com/sharkdp/numbat/issues/66

sharkdp commented 5 months ago

@garywill This sort of thing is now fully supported in Numbat

image

Check the example out online here: https://numbat.dev/?q=fn+invert%28x%29+%3D+1%2Fx%E2%8F%8Efn+parallel_resistors%28resistances%29+%3D+1+%2F+sum%28map%28invert%2C+resistances%29%29%E2%8F%8Eparallel_resistors%28%5B22+k%E2%84%A6%2C+47+k%E2%84%A6%5D%29%E2%8F%8E