jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
517 stars 82 forks source link

Named parameters, related parameters and more Geometry #333

Open Happypig375 opened 6 years ago

Happypig375 commented 6 years ago

(The three 3s special issue! :tada: :clap:)

Take line as an example. Not every time two points are specified, sometimes a point and slope is given. Now line cannot be used. How are we going to solve this? Extend the parameter list?

line(pt1, pt2, x, slope)

Usage:

line([1,2],,,1)

Now this is ugly and ambiguous.

Named parameters will make this better:

line(pt1: [1,2],slope: 1)

I have thought of a few proposals of this syntax (I prefer 1 and 2):

  1. function(param=value) Pro: Looks natural Con: Ambiguity with equations
  2. function(param: value) Pro: Looks natural Con: Ambiguity with ratios
  3. function({param: value}) Pro: Literally familiar JSON Con: Looks unlike math
  4. function{param: value} Pro: Less excess parentheses than 3, looks more natural than 3 Con: Not look like math
  5. function{param=value} Pro: The look is on par with 4 Con: Still not look like math

Related parameters:

Definition: function(x, 40-x)
Usage: function(2, 3) //Throw error!!!
function(,23) // Infer first param as 17

Helps implementing the below.


The above proposals will greatly benefit the following proposal: More Geometry functions extend line function with these arguments:

And new functions:

rectangle(leftup, leftdown, rightup, rightdown, length, width, perimeter, area, rotation) circle(centre, diameter, circumference, area, circumpoint1, circumpoint2, circumpoint3) triangle(point1, point2, point3, side1, side2, side3, angle1, angle2, angle3, perimeter, area) area(equation) perimeter(equation) vertexes(equation) These are the main ones for now, will add more later.

Question: Currently line is in Algebra.js, maybe factor it and the new functions into a new Geometry.js (needs to depend on Solve.js as calculating shape intersections etc. requires Solve.js)? Probably yes

Thanks for reading (and implementing).

Happypig375 commented 6 years ago

Related parameters in my above incomplete form should be used with setFunction:

nerdamer.setFunction("func", ["x", "y=40-x"], "x+log(y)");
jiggzson commented 6 years ago

line(pt1: [1,2],slope: 1)

I definitely like the idea of having named parameters, especially the proposed syntax. I'm not too worried about ambiguity but more about the difficulty of implementation using the current parser.

Question: Should line also create parabolas etc?

I don't know. Provide me with examples of where this is done or a proposal of how this can be supported and we'll give it a go.

Currently line is in Algebra.js, maybe factor it and the new functions into a new Geometry.js (needs to depend on Solve.js as calculating shape intersections etc. requires Solve.js)?

Adding a geometry package shouldn't be a problem. The line function can technically be in the core since it has no dependencies but we can hash this out.

Happypig375 commented 6 years ago

Should line also create parabolas etc?

Just thought of an ambiguity: https://www.wolframalpha.com/input/?i=parabola+through+(1,2),(2,1),(3,3)+and+hyperbola+through+(1,2),(2,1),(3,3) Maybe separate functions for parabolas and hyperbolas.

Happypig375 commented 6 years ago

the difficulty of implementation using the current parser

What about adding this during the rewrite of the parser in the upcoming 0.8.0?

jiggzson commented 6 years ago

@Happypig375 I'm still looking at options for 0.8.0 so I'll be keeping this issue in mind.

Happypig375 commented 6 years ago

A couple of questions to discuss:

I prefer 1 over the rest, then 2, then 3.

The design above is super incomplete. But it can for example link the area parameter to the 3 side parameters in the triangle function (also not to confuse with the triangular function, aka tri).

I see that matrices and vectors in nerdamer are imported from Sylvester. Although it is basically dead, it still has some nice Geometry functions. Can import function ideas from there.