stefnotch / quantum-sheet

QuantumSheet - A user friendly mathematics worksheet and solver
https://stefnotch.github.io/quantum-sheet/
GNU General Public License v3.0
55 stars 5 forks source link

Working With Arrays #50

Open Ephraim-Bryski opened 2 years ago

Ephraim-Bryski commented 2 years ago

I want a flexible approach for working with stuff like arrays, tables, and piecewise functions. Instead of following a set procedure, the user gives constraints and possible solutions are returned. This issue's just on arrays and iteration.

Approach

The Fibonacci numbers would usually be created iteratively like this:

Fib=[]
Fib[0]=0
Fib[1]=1
for (i=2;i<5;i++){
  Fib[i]=Fib[i-1]+Fib[i-2]
}

Instead I’m proposing something like this (these are all equations in a system, order does not matter):

Fib[0]=0
Fib[1]=1
Fib[i]=Fib[i-1]+Fib[i-2]
2<=i<5

Which can then be solved: Fib=[0,1,1,2,3]

This allows you to use any piece of information. For example if you want to make something following the rules of Fibonacci numbers but the 5th element is 6 instead, you would just do:

Fib[0]=0
Fib[4]=6
Fib[i]=Fib[i-1]+Fib[i-2]
2<=i<5

This would return: Fib=[0,3,3,6]

Application

This would be useful for engineering applications, where what is known and unknown varies from case to case. For example, the total pressure from soil layers is determined by adding the weight from each layer:

SoilLayers

p=rho*g*h
p_cum[i+1]=p_cum[i]+p[i]

If instead you wanted to find the depth required to get to a certain pressure with two soil layers, for example, you could do:

SoilLayers
rho=[1200,1700]
g=9.8
h=[15,depth-15]
p_cum[0]=0
p_cum[2]=20000

Basically, by building it around solving systems of equations, you are not constrained to a set of input or outputs as you would be using loops.

A similar approach could be used for piecewise functions and tables, which I could show in another issue.

I think the main challenge is to figure out exactly how the program should interpret arrays and how it should convert it to systems of equations.