-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathold_sheet.js
482 lines (350 loc) · 11.2 KB
/
old_sheet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import React, { useState } from "react";
import { addStyles, EditableMathField, StaticMathField } from "react-mathquill";
import nerdamer from 'nerdamer/nerdamer.core.js'
import 'nerdamer/Algebra.js'
import 'nerdamer/Calculus.js'
import 'nerdamer/Solve.js'
addStyles();
function splitSolveFor(text){
try{
var splitEnd=text.split(")")
var main=splitEnd[0]
var splitMain=main.split("(")
var solveFor=splitMain[0]
var inTermsOfText=splitMain[1]
var inTermsOf=inTermsOfText.split(",")
return [solveFor,inTermsOf]
}catch{
return [text.split("(")[0],[]]
}
}
function solveEqns(eqnsIn,solveFor,inTermsOf){
/* additional stuff:
parse equations (check for one equality, operations, and variables and that's it)
check if no contradiction in equations
check if infinite solutions (a==a shouldn't return 0)
check if solveVars are in equations
if solveFor is empty, just use solveEquations
deal with multiple solutions when substituting (like quadratics)
systems(["x=4*a+b","b=2*a"],"b",["a","x"]) returns "b=-4*a+x" (ignores the next equation)
instead it should keep on going and throw error if there's ever too few variables
*/
if (solveFor.length===0){
if (eqnsIn.length===1){eqnsIn=eqnsIn[0]} // if single equation, nerdamer requires string, not array
try {
var sol=nerdamer.solveEquations(eqnsIn).toString() // need to have it access all equations instead!
if (sol===""){
return "Blank Line (or it cant solve)"
}
} catch {
return "Cannot Solve"
}
// variable name is not included in array if there's only one variable ):
if (!sol.includes(",")){
sol=nerdamer(eqnsIn).variables()[0].concat(",").concat(sol)
}
var splitSol=sol.split(",")
var eqnSol=[]
for (let i=0;i<splitSol.length/2;i++){
eqnSol.push(
splitSol[2*i]
.concat("=")
.concat(splitSol[2*i+1])
)
}
return eqnSol
}
var eqns=[[]]
for (let i=0;i<eqnsIn.length;i++){
eqns[0][i]=nerdamer(eqnsIn[i])
}
//var solveFor="a" // for now you must specify what to solve in terms of
//var inTermsOf=["b"]
var solveVars=[solveFor,inTermsOf].flat()
// function will return the single equation which will then be solved for
var nEqns=eqns[0].length
for (var i=0;i<nEqns-1;i++){
var eqn=eqns[i][i]
//var eqn=nerdamer('simplify('.concat(eqns[i][i]).concat(')'))
var eqnVars=eqn.variables()
var solution
var subVar=excess(eqnVars,solveVars)
if (subVar===undefined){
// this means there's no additional variables in the equation
if (excess(solveVars,eqnVars)===undefined){
// solveVars and eqnVars are equivalent
solution=eqn
break
}else{
return 'error: too many variables to solve in terms of'
}
}
var solvedEqn=eqn.solveFor(subVar)
eqns[i+1]=[]
for (var j=0;j<nEqns-i-1;j++){
eqns[i+1][i+1+j]=eqns[i][i+1+j].sub(subVar,solvedEqn)
}
}
if (solution===undefined){ // solution hasn't already been found OR only one eqn
var finalEqn=eqns[nEqns-1][nEqns-1]
var solutionVars=finalEqn.variables()
if (excess(solveVars,solutionVars)!==undefined){
return 'error: end too many variables to solve in terms of'
}else if (excess(solutionVars,solveVars)!==undefined){
return 'error: end too few variables to solve in terms of'
}else{
solution=finalEqn
}
}
// only returns the first item which is excess
function excess(A,B){
// if it's one variable, nerdamer doesn't use an array, so this puts it in an array:
if (typeof(A)==="string"){A=[A]}
if (typeof(B)==="string"){B=[B]}
return A.find(
itemTestExtra=>B.every(
(itemCompare)=>{return itemTestExtra!==itemCompare}
)
)
}
return solveFor.concat("=").concat(solution.solveFor(solveFor).toString())
}
function computeSheet(sheetData){
// do a check for duplicate names
// for now system names must be in order ):
var newSheetData=sheetData
// clear all of the previous results:
for (let i=0;i<sheetData.length;i++){
var system=sheetData[i]
for (let j=0;j<system.eqns.length;j++){
newSheetData[i].eqns[j].result=""
}
}
var hitError=false
var usedNames=[]
for (let i=0;i<sheetData.length;i++){
var SoE=sheetData[i]
var eqns=SoE.eqns
for (let j=0;j<eqns.length;j++){
try {
var eqn=nerdamer.convertFromLaTeX(eqns[j].eqn).toString()
} catch {
var eqn="NOPE"
}
var result
if(eqn===""){
continue
}
var solveForLaTeX
var inTermsOfLaTeX
var inTermsOf=[]
var solveFor
if (eqns[j].solveText===null){var solveText=""}
else {var solveText=eqns[j].solveText}
[solveForLaTeX,inTermsOfLaTeX]=splitSolveFor(solveText)
try{
var solveFor=nerdamer.convertFromLaTeX(solveForLaTeX).toString()
}catch{
var solveFor=""
}
for (let j=0;j<inTermsOfLaTeX.length;j++){
try{
inTermsOf.push(nerdamer.convertFromLaTeX(inTermsOfLaTeX[j]).toString())
}catch{
inTermsOf.push("")
}
}
//[solveFor,inTermsOf]=splitSolveFor(solveText)
// check if reference:
if (!eqn.includes("=")){
// check if a name on sheet:
if (!usedNames.includes(eqn)){
result=("TEXTerror: ").concat(eqn).concat(" has not been defined")
hitError=true
}else if (usedNames.includes(eqn)){
var names=[]
for (let i=0;i<sheetData.length;i++){names.push(sheetData[i].name)}
var iRef=names.findIndex((name)=>name===eqn)
var system=[]
for (let i=0;i<sheetData[iRef].eqns.length;i++){system.push(sheetData[iRef].eqns[i].result)}
if (eqns[j].operation==="solve"){
result=solveEqns(system,solveFor,inTermsOf)
}else{
result=system
}
}
}else if(eqns[j].operation==="solve"){
result=solveEqns([eqn],solveFor,inTermsOf)
}else{
result=eqn
}
newSheetData[i].eqns[j].result=result
if (hitError){
return newSheetData
}
usedNames.push(SoE.name)
}
}
return newSheetData
}
class Sheet extends React.Component {
// constructor has states with sheet info?
constructor(props){
super(props)
this.state= {
SoEs:[
{
name:"s",
eqns: [
{eqn: "\\rho_d=\\frac{m_s}{V}", operation: "", solveText: null, result: ""},
{eqn: "\\rho=\\frac{m}{V}", operation: "", solveText: null, result: ""},
{eqn: "m=m_w+m_s", operation: "", solveText: null, result: ""},
{eqn: "\\omega=\\frac{m_w}{m_s}", operation: "", solveText: null, result:""}
]
},
{
name:"SoE 2",
eqns: [
{eqn: "s", operation: "solve", solveText: "\\rho_d\\left(\\rho,\\omega\\right)", result:""}
]
}
]
}
}
render(){
var sheetElements=[]
for (let i=0;i<this.state.SoEs.length;i++){
var SoE=this.state.SoEs[i]
//this.SoEs[i].name=newName
sheetElements.push(<Name
onEdit={(newName)=>
{
// should just appl
this.state.SoEs[i].name=newName
this.setState({SoEs: computeSheet(this.state.SoEs)})
}
}
text={SoE}
/>)
for (let j=0;j<SoE.eqns.length;j++){
sheetElements.push(<Line
onEdit={
(newEqn)=>
{this.state.SoEs[i].eqns[j]=newEqn
this.setState({SoEs: computeSheet(this.state.SoEs)})
}
}
addLine={()=>
{
var blankLine={eqn:"",operation:"",solveText: null,result:""}
this.state.SoEs[i].eqns.splice(j+1, 0, blankLine)
this.setState({SoEs: this.state.SoEs})
}
}
removeLine={()=>
{
var blankLine={eqn:"",result:""}
this.state.SoEs[i].eqns.splice(j, 1)
this.setState({SoEs: this.state.SoEs})
}
}
eqnData={SoE.eqns[j]}
/>)
}
}
//console.log(this.state.SoEs)
return(
sheetElements
)
}
}
class Name extends React.Component {
render(){
//const name=<tr><td></td></tr>
const editField=<input
type="text"
value={this.props.text.name}
onChange={(e)=>{this.props.onEdit(e.target.value)
}
}
>
</input>
return <tr><td>{editField}</td></tr>
}
}
class Line extends React.Component {
constructor(props){
super(props)
}
breakUp(newText){
var newEqnData=this.props.eqnData
newEqnData.eqn=newText
if(newText.includes("solve")){
newEqnData.operation="solve"
newEqnData.eqn=newText.replace("solve","")
}
if(newText.includes("for")){
newEqnData.eqn=newText.replace("for","")
newEqnData.solveText=""
}
return newEqnData
}
render() {
var eqnData=this.props.eqnData
if (eqnData.result.includes("TEXT")){
var resultDisplay="\\text{".concat(eqnData.result.replace("TEXT","")).concat("}")
}else{
if (typeof eqnData.result==="string"){
eqnData.result=[eqnData.result]
}
var resultDisplay=[]
for (let i=0;i<eqnData.result.length;i++){
resultDisplay.push(
<StaticMathField>
{nerdamer.convertToLaTeX(eqnData.result[i])}
</StaticMathField>,
)
resultDisplay.push(<br/>)
}
}
var inputField=[]
var eqnField=<EditableMathField
latex={this.props.eqnData.eqn}
onChange={(newField)=>{this.props.onEdit(this.breakUp(newField.latex()))}}
/>
var solveForField=<EditableMathField
latex={this.props.eqnData.solveText}
onChange={(newField)=>{
var newEqnData=this.props.eqnData
newEqnData.solveText=newField.latex()
this.props.onEdit(newEqnData)
}}
/>
if (this.props.eqnData.operation==="solve"){
inputField.push(<StaticMathField>{"\\text{solve }"}</StaticMathField>)
}
inputField.push(eqnField)
if (!(this.props.eqnData.solveText===null)){
inputField.push(<StaticMathField>{"\\text{for }"}</StaticMathField>)
inputField.push(solveForField)
}
const line=<tr>
<td>
<button onClick={
()=>{this.props.addLine()}
}>+</button>
<button onClick={
()=>{this.props.removeLine()}
}>-</button>
</td>
<td>
{inputField}
</td>
<td>
{resultDisplay}
</td>
</tr>
return line
}
}
export default Sheet