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

Approach For Working With Equations #47

Open Ephraim-Bryski opened 3 years ago

Ephraim-Bryski commented 3 years ago

My approach is to use just equality, solve and substitute for algebraic manipulation. I think that would be sufficient and make it simple to use. as well as easy to build off of.

I know this is very different from the MathCAD approach, so we'll have to figure out what would work best.

Layout

Each block contains a system of equations. The user types stuff in on the left side and the result is shown on the right side. Farther right than that, it could potentially show numeric results if there are any. layout

Scopes

For each line in the block, you can type an equation or the name of another block. In this way, you can create nested scopes, where in this case, Block2 has access to Block1 and Block3 has access to Block2 and Block1. scopes

Evaluation

Numeric evaluation would occur automatically on the result side: eval

Assignment

The equivalent of assignment can be performed as shown: preview which is analogous to: Screenshot 2021-08-29 171807

Functions

The equivalent of functions would be created as shown: myfunc The advantage of this is that the "function" can consist of a system of equations and you could solve for any variable. I think this would be useful if we want to make libraries for engineering, where we would write the equations, and the user would choose how to combine them and what to solve for.

stefnotch commented 3 years ago

This approach is super interesting! I'm definitely thinking about how to integrate something like it, what the implications would be, which parts would end up being much neater and which parts would be less than ideal.

Here are some initial thoughts, mainly from the perspectives of a software engineer and also someone who likes mathematics

Ephraim-Bryski commented 3 years ago

Here's how I imagine the scopes would work in some more detail.

All variables in a block are local to that block. The name of the block though can be referenced from any other block. When you reference Block1 from Block2 say, the equations in Block1 become part of Block2, and so Block2 has access to Block1 variables.

If contradictory equations are in the same block, it will say there's a contradiction when it previews the solution:

Input Block1 Preview
a=1 a=1 contradiction
a=2 a=2

If they're in separate blocks, there is no issue as their scopes are local to that block.

Input Block1 Preview
a=1 a=1
Input Block2 Preview
a=2 a=2

I don't see how it would leak variables into the global scope, although there might be something I'm missing.

Ephraim-Bryski commented 3 years ago

You persuaded me that functions would be good to have. (The derivative example could be done without it, but it would require 4 blocks, which would be pretty ugly). I think the simplest approach would be to have an expression default as a function. The block would contain a single expression (only one would be allowed, or else the function would have multiple definitions) and it's name would be the name of the function.

So this would be your example:

Input f Preview
x^2+2x+7 f(x)=x^2+2x+7
Input sol Preview
y=f'(7) y=16

The name "f" would then be accessible from all other blocks since it's the name of a block. The user could then plug in values of x or do operations on the function to create a new function.

This would also solve some other issues:

stefnotch commented 3 years ago

This seems like a super interesting approach! I find it really nice how it generalizes to other things, like implicit functions. Implicit function: image of an implicit function

Another thing that would 'just work' would be this image (This one is a made up example, but I did see examples along those lines in my university math courses)

So, I went though my notes and my mathematics exercises and found a few more things that we could eventually support. I'd be delighted to hear your thoughts on those things:

Functions

Text

We need text blocks. Is there a shortcut or anything you'd suggest?

Unevaluated math/LaTeX

A simple LaTeX math block, where the user can type anything they want and QuantumSheet will just ignore it would be useful. It would mainly be used for documenting something or jotting down some very fancy mathematical notes.

Tables

Should we have a custom block type for tables?

Expression rewriting and solving with steps

Those features are features for the far future.

Optional auto evaluation

Auto evaluation is definitely super interesting, but there are times when an advanced user might just want to turn it off

stefnotch commented 3 years ago

@phcreery If you've got any thoughts on this matter, I'd love to hear them! Or if you have any interesting examples, please do share them

phcreery commented 3 years ago

To regurgitate, you are proposing using = for equality, solve and assignment.

Input | Type | Result

Here's an interesting case:

From my perspective, this can be very confusing, for the user and computer to understand. I think it is quite standard to use a different expression for "assign". Even my TI-nspire calculator uses := for assigning/storing variables/functions. Programming languages also use techniques to differentiate between asking and assigning.

In the case of == (thick = in mathcad) or similar (∈), it is imposing a restriction and needs more context for the system to understand/evaluate. i.e. System of Equations. In this case it will be used inside an If statement, function, solve block, etc.

Ephraim-Bryski commented 3 years ago

To stefnotch:

I think a lot of those are neat ideas, some of which I've thought about a lot, some less so. I think we can make separate issues for those.

I wasn't really thinking of using recursive functions (I'm afraid that could get really messy). However, for something like the Fibonacci numbers, I was thinking of a block like:

F[1]=0 F[2]=1 F[i+2]=F[i+1]+F[i] i<50

Where F is an array (I'll make an issue where I'll say why I think that's better than having a for loop keyword).

I definitely want to add piecewise functions and tables, and I thought about (and started working on) how to implement them.

Text blocks would be nice and pretty easy I think (maybe just some button to click for it).

I haven't thought about anonymous functions; maybe you could talk about that a bit more.

Adding steps might be nice.

Ephraim-Bryski commented 3 years ago

To phcreery:

"To regurgitate, you are proposing using = for equality, solve and assignment."

Not quite (I may have said something like this before, which is my bad, the terminology gets a bit messy). Essentially naming a block is assignment. I would want to use the "solve" keyword to solve.

The basic reason for using this approach is I would want to make sheets of common (likely engineering) equations (you could call them libraries) which could be easily added by other people to their work. There has to be a way of easily naming systems of equations, substituting in values, solving for what you want, and using the results later. This could get very messy, so this provides a simple, easy to read framework for users.

I realize the best way of showing examples is through a Google Sheet; here's one with your examples. We can then just add more cases and comment on them easily.

stefnotch commented 3 years ago

@Ephraim-Bryski

Recursive functions

I wasn't really thinking of using recursive functions (I'm afraid that could get really messy). However, for something like the Fibonacci numbers, I was thinking of a block like:

F[1]=0 F[2]=1 F[i+2]=F[i+1]+F[i] i<50

Where F is an array

Recursive functions usually pop up when a problem is best specified in terms of itself. One place where I encountered them was in algorithmics class, when trying to figure out how fast an algorithm is. For example, merge sort, which can be used for sorting a list of numbers, is a recursive algorithm. Its runtime is approximately T(n) = 2*T(n/2) + n, where n is the size of the list. Notice how it doesn't follow the simple pattern of always taking the T(n-1) value. Instead, it takes the T(n/2) value!

Because a recursive function can be a nightmare to calculate, there is the process of finding a 'closed form' solution. When it works out, everyone is happy, because you end up with a slightly unwieldy, but perfectly reasonable solution.

Failing that, one can try to approximate the closed form solution. For merge sort, this ends up being approximately n * log(n)

I hope this is some useful background info on recursive functions. Mind elaborating what you have in mind with the array option?

Equality

phcreery raised an interesting point with 3 = 3 "equality" 3 = 3 = True. In boolean algebra, the equals sign does not mean "the two sides are definitely equal" but rather means "are the sides equal?". As someone who is studying informatics, thinking about supporting boolean algebra would make me pretty happy.

Depending on the context, an equals sign can mean a lot of different things:

On that note, the 3blue1brown contest submission uses re-assigning. It takes the current price and adds something to it.

image

I took the liberty of adding some more examples to your worksheet. Let me know what you think

Anonymous functions

I haven't thought about anonymous functions; maybe you could talk about that a bit more.

It's a function that one defines inline, whenever they need a function. It practically never comes up in mathematics, but comes up all the time in computer science.

The computer science example is, you have a list of fruits. And you have a cute sorting algorithm. But your sorting algorithm has no idea how it should sort the fruits. (By weight? By name? By color?) You decide you want to sort them by weight, so you write something like sortedFruits = sort(fruits, (a, b) => a.weight < b.weight )

stefnotch commented 3 years ago

I just noticed that there is a somewhat standardized notation for a system of equations

system of equations

It's surprisingly similar to the typical notation for piecewise functions

piecewise function

This doesn't cover anywhere near all of the proposal, but I figured it'd be interesting to note down nevertheless.

phcreery commented 3 years ago

@Ephraim-Bryski

I think I see now. So essentially, everything is named. It kinda reminded me of programming functions where every block has a name.

The way Mathcad does this is by using a "Program" symbol "||" (two vertical bars). Here is an example of one of the many ways I have used it. http://160592857366.free.fr/joe/ebooks/Mechanical%20Engineering%20Books%20Collection/METROLOGY/Dimensioning%20and%20Tolerancing%20Handbook/81314_09.pdf

image

This function is fed the nominal dimensions and respective tolerances stacks as single column matrices. It then returns the nominal distance, the (furthest) worst-case distance, the MRSS distance, and the RSS distance. The function can then be called like image

With this function defined (or imported), anyone can call EvalTol() anywhere to instantly get values for various tolerancing methods.

Ephraim-Bryski commented 3 years ago

@phcreery

Yeah that's the idea. You would either name a system of equations or (after stefnotch's suggestion) a function.

The motivation behind this approach is to create an environment where users can easily build off of other work -- I think it opens up a lot more possibilities for something cloud-based.

For example, we might want to have the equation for Reynold's number for users. We don't know what the user would solve for and how they will use it, so we should have an equation named Reynolds with the equation as opposed to just a function which outputs the value. That way, they could, for example, find the required viscosity and then use that value to find what liquid they should choose from a table they added to the sheet as well. Others could then build off of that procedure.

Software like MathCAD seem good for a specific project, but I imagine you would have trouble using that work for a different project.

(I notice that MathCAD example is more of a programming function. At least for the example shown, I think that could be done easily using a system of equations and the solve keyword on the name of that system).

(I also think that functions alleviate the need for the substitute keyword and previewing as well. This means it should only require system of equation definition, function definition, and the solve keyword, which I think is a pretty elegant approach).

phcreery commented 3 years ago

@Ephraim-Bryski

Oh okay, that's actually an impressive approach. So, instead of functions with pre-defined parameters (in the case of Reynolds number, up to 4 variables) and a predefined metric to output (Re), you import the entire "equation" and it can be solved for any of the variables (Re, density, flow speed. length, etc).

Again, playing the devil's advocate :), what if you use 2 equations with overlapping variable names?

Ephraim-Bryski commented 3 years ago

I'm assuming in this example the user doesn't want the variables to refer to the same thing.

The user would just put the two equations in separate blocks (they are each their own system of one equation). The user could substitute in different values for the variable by adding equations to the systems (e.g. "x=3" and "x=4") Then the user can solve each of these equations in separate blocks and the results would also be separated.

If the user wants the variable name to refer to the same variable, they can put them in the same block (a single system). They can substitute a value for the variable by adding an equation (e.g. "x=3") and solve the system.

If the user wants the equations to be separate but substitute a value for all cases of the variable (maybe they want some variables to refer to the same thing but not others), they can create a function block assigning a value to that variable.

phcreery commented 3 years ago

I see. I would love to see this implemented as it is very useful.

Ephraim-Bryski commented 3 years ago

That's great to hear; the code I wrote still needs to be fixed up a bit, functions need to be added, and it would have to be incorporated into this project.