PaddiM8 / kalker

Scientific calculator with math syntax that supports user-defined variables and functions, complex numbers, and estimation of derivatives and integrals
https://kalker.xyz
MIT License
1.59k stars 70 forks source link

Add piecewise functions #33

Closed abrudz closed 3 years ago

abrudz commented 3 years ago

For example image could be written as

f(x) = {-x-x if x≤-3; x+3 if -3≤x≤0; 3-2x if 0≤x≤3; 0.5x-4.5 if 3≤x}

or:

f(x) = {x≤-3: -x-x; -3≤x≤0: x+3; 0≤x≤3: 3-2x; 3≤x: 0.5x-4.5}
PaddiM8 commented 3 years ago

Yeah this should be a thing. Might even be possible to have it span multiple lines. The desktop program techncially already lets you write multi-line expressions if you press enter before closing a bracket. Could end up being quite readable!

PaddiM8 commented 3 years ago

grafik

Looks like this right now. Eventually I also want to format it like this automatically (well, on the web version only probably), and make semicolons optional when it is spanned over several lines.

abrudz commented 3 years ago

Cool. So does this work?

f(x)={
  1 if x≤1
  f(x-1)+f(x-2) if x>1
}
PaddiM8 commented 3 years ago

Cool. So does this work?

f(x)={
  1 if x≤1
  f(x-1)+f(x-2) if x>1
}

Not yet, but that's actually the next thing I'm planning to implement, if things go as planned! (just added otherwise as well)

PaddiM8 commented 3 years ago

@abrudz Seems to be working fairly well now from what I can tell. Your example worked, and I was able to make a function to get fibonacci numbers! You have been very helpful, thank you.

Because of the nature of this kind of change, I'll make sure it gets tested a bit more before I push it.

PaddiM8 commented 3 years ago

cf869358f29fa55adc217415796d3768e4964069
68e07699422d877d523860fcb03d806ed55006e0