This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spdFuncs_v21.0.wl
1964 lines (1790 loc) · 71.7 KB
/
spdFuncs_v21.0.wl
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* ::Package:: *)
BeginPackage["spd`"];
Unprotect @@ Names["spd`*"];
ClearAll @@ Names["spd`*"];
ClearAll @@ Names["spd`Private`*"];
ClearAll @@ ToExpression@Evaluate@Select[Names["Global`*"],StringContainsQ[#,"spd"]&];
spdPlot::usage = "Speed Plot
The clean, user-friendly plot function.
Inspired by Detailed Plot and Joseph's GigaChadPlot.
Check out https://rotaryviper.github.io/JMSS-Mathematica-Hub/ for other mathematica functions.
You can do whatever you want with this code. Use it, fix it, edit it, share it. This is yours to keep :)
How to use:
Initialise the functions
1. Open the spdFuncs notebook.
2. Click within the notebook code cell.
3. Shift + Enter to initialise the functions.
Using the functions
1. Click into any other notebook.
2. Type: spdPlot[x]
3. Shift + Enter to run the function.
4. Watch magic happen :)
5. Check/Uncheck the checkboxes on the left as you desire.
Find Function
1. Type any number or numeric symbol you want. Eg: 2, -\[ExponentialE], \!\(\*FractionBox[\(\[Pi]\), \(2\)]\), 5\!\(\*SqrtBox[\(3\)]\)
2. Click enter.
3. Dots will appear on the X or Y coordinate on any graph.
Syntax:
Its the exact same as the mathematica Plot function.
Note:
Type smaller equations are on the left, bigger equations are on the right. It runs faster that way.
This plot function can only calculate real numbers.
I tried keeping the function as similar to Plot as possible. But I'm still improving everyday.
For users before v19, Incremental has been depreceated.
Sharing is caring - Rotary Viper";spdInverse::usage="Find Inverse
spdInverse[ expr_ , dom_:x ]
Enter a single expression into expr_
(Optional)Enter a symbol into dom_ to use as the domain
Swap the x and y around";spdFindX::usage="Find X
spdFindX[ equ_ , xCoord_ , dom_:x , rangeLow_:-\[Infinity] , rangeHigh_:\[Infinity] ]
Enter a single expression or list of expressions into equ_
Enter a number into xCoord_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a number into rangeLow_ to use as the lower range
(Optional)Enter a number into rangeHigh_ to use as the upper range
Sub the number into the expression(s)
FullForm:
Select[Flatten@Cases[{equ/.dom->xCoord},_?NumericQ],FreeQ[#, _Complex]&&rangeLow<=#<=rangeHigh&]
SimpleForm:
equ/.dom->xCoord";spdFindY::usage="Find Y
spdFindY[ equ_ , yCoord_ , dom_:x , domLow_:-10 , domHigh_:10 ]
Enter a single expression or list of expressions into equ_
Enter a number into yCoord_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a number into domLow_ to use as the lower domain
(Optional)Enter a number into domHigh_ to use as the upper domain
Solve for domain with the y coordinate and equation
FullForm:
Solve[equ==yCoord&&domLow<=dom<=domHigh,dom,Reals]
SimpleForm:
Solve[equ==yCoord,dom,Reals]";spdFindGrad::usage="Find Gradient
spdFindGrad[ equ_ , gradient_ , dom_:x , domLow_:-10 , domHigh_:10 , rangeLow_:-\[Infinity] , rangeHigh_:\[Infinity] ]
Enter a single expression or list of expressions into equ_
Enter a number into gradient_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a number into domLow_ to use as the lower domain
(Optional)Enter a number into domHigh_ to use as the upper domain
(Optional)Enter a number into rangeLow_ to use as the lower range
(Optional)Enter a number into rangeHigh_ to use as the upper range
Solve for x and y coordinates when derivative=0 and domain and range are within their allocated constraints
FullForm:
Solve[D[equ,dom]==gradient&&equ==y&&domLow<=dom<=domHigh&&rangeLow<=y<=rangeHigh,{dom,y},Reals]
SimpleForm:
Solve[D[equ,dom]==gradient&&equ==y,{dom,y},Reals]";spdPOI::usage="Find Points of Intersection
spdPOI[ equ_ , dom_:x , domLow_:-10 , domHigh_:10 , rangeLow_:-\[Infinity] , rangeHigh_:\[Infinity] ]
Enter a list of expressions into equ_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a number into domLow_ to use as the lower domain
(Optional)Enter a number into domHigh_ to use as the upper domain
(Optional)Enter a number into rangeLow_ to use as the lower range
(Optional)Enter a number into rangeHigh_ to use as the upper range
Solve for x and y coordinates of every expression provided and domain and range are within their allocated constraints
Select[{dom,y}/.Solve[equList[[equ1]]==equList[[equ2]]==y&&domLow<=dom<=domHigh&&rangeLow<=y<=rangeHigh,{dom,y},Reals],NumericQ[#[[1]]]&];
SimpleForm:
Solve[equList[[equ1]]==equList[[equ2]]==y,{dom,y},Reals]";spdAsyV::usage="Vertical Asymptotes
spdAsyV[ equList_ , dom_:x , domLow_:-10 , domHigh_:10 ]
Enter a list of expressions into equList_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a number into domLow_ to use as the lower domain
(Optional)Enter a number into domHigh_ to use as the upper domain
2 methods of finding vertical asymptotes are used:
Mathematica's way
FullForm:
1. Table[FunctionDomain[{equList[[$i]],domLow-1<=dom<=domHigh+1},dom],{$i,Length[equList]}]
SimpleForm:
FunctionDomain[equ,dom]
How to do it mathematically
2. Solve[1/equList[[$i]]==0&&domLow<=dom<=domHigh]
Combine the results of both then delete duplicates";spdAsyO::usage="Other Asymptotes
spdAsyO[ equList_ , dom_:x ]
This use to be the most broken speed function
Now resource function is used instead
Enter a list of expressions into equList_
(Optional)Enter a symbol into dom_ to use as the domain
FullForm:
Table[Simplify@PolynomialQuotient[Numerator[equList[[$i]]],Denominator[equList[[$i]]],dom],{$i,Length[equList]}]
SimpleForm:
PolynomialQuotient[Numerator[equ],Denominator[equ],dom]";spdFindTangent::usage="Find Tangent for given x
spdFindTangent[ equList_ , xCoord_ , dom_:x ]
Enter a list of expressions into equList_
Enter a number into xCoord_
(Optional)Enter a symbol into dom_ to use as the domain
Find gradient by subbing xCoord into derivative of expression
grad=D[equ,dom]/.dom->xCoord
Find yCoord by subbing xCoord into original expression
yCoord=equ/.dom->xCoord
Find tangent by applying the gradient along with horizontal and vertical translations until both equations touch once
tangent=grad(dom-xCoord)+yCoord
This is inverse Find Normal";spdFindNormal::usage="Find Normal for given x
spdFindNormal[ equList_ , xCoord_ , dom_:x ]
Enter a list of expressions into equList_
Enter a number into xCoord_
(Optional)Enter a symbol into dom_ to use as the domain
Find gradient by subbing xCoord into derivative of expression
grad=-1/D[equ,dom]/.dom->xCoord
Find yCoord by subbing xCoord into original expression
yCoord=equ/.dom->xCoord
Find tangent by applying the gradient along with horizontal and vertical translations until both equations touch once
tangent=grad(dom-xCoord)+yCoord
This is inverse Find Tangent";spdEndPoints::usage="Find endpoints for given x
spdFindNormal[ equ_ , dom_:x , domLow_:-10 , domHigh_:10 , rangeLow_:-10 , rangeHigh_:10 ]
Enter a list of expressions into equ_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a number into domLow_ to use as the lower domain
(Optional)Enter a number into domHigh_ to use as the upper domain
(Optional)Enter a number into rangeLow_ to use as the lower range
(Optional)Enter a number into rangeHigh_ to use as the upper range
Solve top, bottom, left and right walls
This is find edges and ";spdHoles::usage="Yes";spdCompleteTheSquare::usage="This function gives you the completed square form of any 3 terms.
The formula used is: \!\(\*SuperscriptBox[\(ax\), \(2\)]\)+bx+c.
Put your terms in the corresponding symbols.
spdCompleteTheSquare[a,b,c,False]
The final argument may be True or False. This chooses if the function gives you steps on how to complete the square.";spdPlotFast::usage="Foundation plot function for spdPlot
Syntax:
Its the exact same as the mathematica Plot function.";spdPlotIntegrate::usage="Plot integal of given expression(s)
Syntax:
Its the exact same as the mathematica Plot function.";spdPlotDerivative::usage="Plot derivative of given expression(s)
Syntax:
Its the exact same as the mathematica Plot function.";spdPlotInverse::usage="Plot inverse of given expression(s)
Syntax:
Its the exact same as the mathematica Plot function.
Please use:
spdInverse[equ]
To find answers";spdPlotCore::usage="spdPlot for core math.
It calculates:
X & Y intercepts
Turning Points
Horizontal & Vertical asymptotes
Syntax:
Its the exact same as the mathematica Plot function.";spdQuestionDerivatives::usage="Make a list of fast derivative questions.
spdQuestionDerivatives[ equ_ , dom_:x , range_:Range[20] , length_:45 , row_:4 , name_: ''Derivates Speed Run'' ]
The default length and row are perfect for making the most out of each a4 piece of paper
Enter a list of expressions into equ_
(Optional)Enter a symbol into dom_ to use as the domain
(Optional)Enter a list of numbers into range_ to sub into the symbols that aren't domain in the list of expressions
(Optional)Enter a number into length_ for each run
(Optional)Enter a number into row_ for the number of runs
(Optional)Enter a string into name_ to name runs";
(*Copy equation*)
spdCopyThis[equ_,message_:spdStyle["Copy This"]]:=Button[message,CopyToClipboard[equ]];
(*Tells you an error*)
spdErrorChecker=Quiet@Check[#,spdStyle@"Error"]&;
(*-------------------------Quality of life functions-------------------------*)
(*Execute a new mathematica process*)
spdNewNoteBook:=SystemOpen[FileNameJoin[{$InstallationDirectory,"Mathematica"}]];
(*Convert equation into clean TraditionalForm*)
spdForm[expr_]:=StandardForm@TraditionalForm@expr;
(*-------------------------Global constants-------------------------
Customise these to your heart's content.
Restart mathematica when changing these options, or they many not apply.
*)
spdFont="Nunito";
spdColourPlot=Automatic;
spdColourPoint1=Red;
spdColourPoint2=Green;
spdColourPoint3=Blue;
spdColourPoint4=Black;
spdColourLine=Red;
spdRoundPrecision=MachinePrecision;
spdMessage="Sharing is Caring";
spdPlotSize=Medium;
spdPlotRange=Automatic;(*Just like a persistent PlotRange*)
spdDragSens=.003;
spdPlotFaster[equ_,dom:{_,_?NumericQ,_?NumericQ}:{x,-10,10},plotOptions:OptionsPattern[]]:=
Plot[Evaluate@Flatten@{equ},dom,Evaluate@FilterRules[{plotOptions},Options@Plot],Background->spdColourPlot,AspectRatio->1,PlotLegends->"Expressions",ImageSize->spdPlotSize,PlotLabel->spd`spdStyle[spdMessage]];
(*-------------------------Processing-------------------------
Enter any formula, points then get the graph
List of formulas*)
spdLinear=a x+b;spdQuad=a x^2+b x+c;spdCubic=a x^3+b x^2+c x+d;spdQuartic=a x^4+b x^3+c x^2+d x+e;
spdEpicyclePlay[img_,colour_:Automatic]:=
Module[{a=spd`Private`spdEpicycleCore[img,colour]},(*spdEpicyclePlay[img,colour]=*)ListAnimate[Flatten@{a,Table[a[[-1]],20],Reverse@a},60]
];
spdEpicycleGIF[img_,colour_:Automatic]:=
Module[{a=spd`Private`spdEpicycleCore[img,colour]},(*spdEpicycleGIF[img,colour]=*)Export["epicycle.gif",Flatten@{a,Table[a[[-1]],20],Reverse@a},"DisplayDurations" ->1/60]
];
(*Enter any formula, points then get the graph*)
spdFormula[formula_,points_,x_:x]:=
With[
{
(*Find all the variables*)
$variables=
Complement[
DeleteDuplicates@Cases[formula,_Symbol,Infinity],
{x}
]
},
Unprotect[spdFormula];
(*Sub values into variables of original formula*)
spdFormula[formula,points,x]=
spdEquPreview
[
formula/.
Solve
[
Table
[
(formula/.x->points[[point]][[1]])==points[[point]][[2]],
{point,Length[points]}
],
$variables,Reals
][[1]]
]
];
(*Enter any formula, points then get the graph*)
spdFormula2[formula_,points_,x_:x]:=
With[
{
(*Find all the variables*)
variables=
Complement[
DeleteDuplicates@Cases[formula,_Symbol,Infinity],
{x}
]
},
Unprotect[spdFormula2];
(*Sub values into variables of original formula*)
spdFormula2[formula,points,x]=
spdEquPreview[
Simplify[
formula/.
Solve[
points,
variables,Reals
]
]
]
];
Begin["`Private`"];
(*-------------------------StartUp quality of life things-------------------------*)
(*Functions to make listable*)
SetAttributes[{spdFindX,spdFindY,spdFindGrad,spdFindTangent,spdFindNormal,spdEndPoints,spdHoles,spdStyle,spdDesectFunction,spdEquPlotG,spdEquPlotGInverse,spdInverse,spdAsyO},Listable];
SetAttributes[{spdSwapHeads,spdPlot,spdPlotCore,spdQuestionDerivatives},HoldAll];
(*Protect certain values for spdPlot*)
(*
spdPlotVariables={equList,equInput,initPos,finalPos,idomLow,idomHigh,domLow,domHigh,irangeLow,irangeHigh,rangeLow,rangeHigh,findXInput,findXBool,findXG,findYInput,findYBool,findYG,findGradInput,findGradBool,findTangentInput,findGradG,findTangentInput,findTangentBool,findTangentG,findNormalInput,findNormalBool,findNormalG,asyBool,asyG,poiBool,poiG,epBool,epG,inverseBool,inverseG,derivativeBool,derivativeG,integrateBool,integrateG};
*)
(*-------------------------Small functions-------------------------*)
spdAltPlotLabel=PlotLabels->Placed[
(*If there is more than one expression, use a list. Otherwise do not use a list*)
If[#2>1,
Table[spdStyle@#,#2],
spdStyle@#
],
{{Scaled[.9],Before}}
]&;
(*Placed[Table[spdStyle@"Derivative",5],{{Scaled[.9],Before}}]*)
spdInputField=InputField[#1,Evaluate@FilterRules[{FrameMargins->None,ContentPadding->False,Alignment->{Left,Center},FieldSize->#2},Options@InputField]]&;
spdAsyParser=Map[#[[1]][[2]][[2]]&,Evaluate@#]&;
spdDesectFunction[expr_]:={
Module[{
(*Check head of expression*)
context=Context@Evaluate@Head@Unevaluated@expr,
head=Evaluate@Head@Unevaluated@expr
},
(*If head of expression is in the global context*)
If[context==="Global`",
Return[Extract[DownValues[Evaluate@OwnValues[head][[1]][[2]]],{1,2},Unevaluated],spdDesectFunction],
Return[Unevaluated@Unevaluated@expr,spdDesectFunction]
]
]
}
(*I didn't write any of this. Thank you Tim*)
spdEpicycleCore[img_,colour_:Automatic]:={
Module[{img1=img,pts,center,toPt,cf,z,m,cn,f,g,r,theta,index,p,circles,anims,gif},{
img1 = Binarize[img1~ColorConvert~"Grayscale"~ImageResize~500~Blur~3];
pts = DeleteDuplicates@
Cases[Normal@
ListContourPlot[Reverse@ImageData[img1],
Contours -> {0.5}], _Line, -1][[1, 1]];
center = Mean@MinMax[pts] & /@ Transpose@pts;
pts = # - center & /@ pts[[;; ;;20]];
SetAttributes[toPt,Listable];
toPt[z_]:=ComplexExpand[{Re@z,Im@z}]//Chop;
cf=Compile[{{z,_Complex,1}},Module[{n=Length@z},1/n*Table[Sum[z[[k]]*Exp[-$i*$i*k*2 Pi/n],{k,1,n}],{$i,-m,m}]],RuntimeOptions->"Speed"];
z=pts[[All,1]]+$i*pts[[All,2]];
m=60;
cn=cf[z];
{f[t_],g[t_]}=Sum[cn[[j]]*Exp[$i*(j-m-1)*t],{j,1,2 m+1}]//toPt;
r = Abs /@ cn;
theta = Arg /@ cn;
index = {m + 1}~Join~
Riffle[Range[m + 2, 2 m + 1], Reverse[Range[1, m]]];
p[t_] = Accumulate@Table[cn[[j]]*Exp[$i*(j - m - 1)*t], {j, index}] // toPt;
circles[t_] =
Table[Circle[p[t][[$i-1]], r[[index[[$i]]]]], {$i, 2, 2 m + 1}];
anims = ParallelTable[
ParametricPlot[{f[s], g[s]}, {s, 0, t}, AspectRatio -> Automatic,
Epilog -> {circles[t], Line[p[t]], Point[p[t]]},
PlotRange -> {{-250, 250}, {-250, 250}},
ImageSize -> 500,Axes->False,PlotStyle->colour],
{t, Subdivide[0.1, 2 Pi, 100]}];
(*spdEpicycleCore[img,colour]=*)gif=Flatten@{anims,Table[anims[[-1]],20],Reverse@anims};
Return[
gif,
spdEpicycleCore]
}]
};
(*Inverse any equation*)
spdInverse[expr_,dom_:x]:={
Module[{result=spdErrorChecker@Unevaluated@TableForm[Simplify[y/.Solve[dom==(expr/.dom->y),y,Reals]]]},
Unprotect[spdInverse];
spdInverse[Unevaluated@expr,dom]=result;
Protect[spdInverse];
Return[result,spdInverse]
]
};
(*Find any X point on graph*)
spdFindX[equ_,xCoord_,dom_:x,rangeLow_:-\[Infinity],rangeHigh_:\[Infinity]]:=
{
Quiet@Module[
{
yCoord,
result={}
},
{
(*Compute yCoord, and check if yCoord is even on the graph
Select removes complex numbers*)
yCoord=
Select[
Flatten@Cases[
Quiet@{equ/.dom->xCoord},_?NumericQ
],
FreeQ[
#, _Complex
]&&
Floor@rangeLow<=#<=Ceiling@rangeHigh
&];
(*Return something if yCoord is not empty*)
If[
yCoord=={},
{},
(*Return all xCoords with yCoords. Delete duplicates
Include gradient
Caches results for spdPlot*)
result=
Simplify@
{
Defer@equ,DeleteDuplicates@
Table[
{
xCoord,yCoord[[$i]],
D[equ,dom]/.dom->xCoord
},
{$i,Length[yCoord]}
]
}
],
Unprotect[spdFindX];
spdFindX[Unevaluated@equ,xCoord,dom,rangeLow,rangeHigh]=result;
Protect[spdFindX];
Return[result,spdFindX]
}
]
};
(*Find any Y point on graph*)
spdFindY[equ_,yCoord_,dom_:x,domLow_:-10,domHigh_:10]:=
{
(*Find any point on the graph*)
Quiet@Module[
{
(*Compute xCoord, and check if xCoord is even on the graph*)
xCoord=
Flatten@
Cases[
dom/.
Solve[
equ==yCoord&&
Floor@domLow<=dom<=Ceiling@domHigh,
dom,Reals
],
_?NumericQ
],
result={}
},
{
(*Return something if xCoord is not empty*)
If[
xCoord=={},
{},
(*Return all xCoords with yCoords. Delete duplicates
Caches results for spdPlot*)
result=
{
Defer@equ,Simplify@DeleteDuplicates@
Table[
{
xCoord[[$i]],
yCoord,
D[equ,dom]/.dom->xCoord[[$i]]
},
{$i,Length[xCoord]}
]
};
];
Unprotect[spdFindY];
spdFindY[Unevaluated@equ,yCoord,dom,domLow,domHigh]=result;
Protect[spdFindY];
Return[result,spdFindY]
}
]
};
(*Find gradients*)
spdFindGrad[equ_,gradient_,dom_:x,domLow_:-10,domHigh_:10,rangeLow_:-\[Infinity],rangeHigh_:\[Infinity]]:=
{
Quiet@Module[
{
cp,
result,
y
},
{
(*Iterate over all lists*)
cp=
Select[
{dom,y}/.
Solve[
D[equ,dom]==gradient&&
equ==y&&
Floor@domLow<=dom<=Ceiling@domHigh&&
Floor@rangeLow<=y<=Ceiling@rangeHigh,
{dom,y},Reals
],
NumericQ[#[[1]]]
&];
(*Output cp X and Y, along with the equation
Caches results for spdPlot*)
result=
Simplify@
{
Defer@equ,
Table[
Flatten@
{
cp[[$i]],
gradient
},
{$i,Length[cp]}
]
};
Unprotect[spdFindGrad];
spdFindGrad[Unevaluated@equ,gradient,dom,domLow,domHigh,rangeLow,rangeHigh]=result;
Protect[spdFindGrad];
Return[result,spdFindGrad]
}
]
};
(*Find points of intersection*)
spdPOI[equList_,dom_:x,domLow_:-10,domHigh_:10,rangeLow_:-\[Infinity],rangeHigh_:\[Infinity]]:=
{
(*Find any point on the graph*)
Quiet@Module[
{
poi,
result,
y,
equListC=Table[Extract[Hold[equList],{1,i},Defer],{i,Length[equList]}]
},
{
(*Make sure equList is longer than 1*)
If[
Length[equList]>1,
(*Calculate the intersection
Caches results for spdPlot*)
result=
DeleteDuplicates@Flatten[
(*Parallel Tables caused too much trouble*)
Table[
(*Plz stay silent, even in parallel processing*)
Table[
{
(*Remove lines that don't intersect*)
poi=
Select[
{dom,y}/.
Solve[
equList[[equ1]]==equList[[equ2]]==y&&
Floor@domLow<=dom<=Ceiling@domHigh&&
Floor@rangeLow<=y<=Ceiling@rangeHigh,
{dom,y},Reals
],
NumericQ[#[[1]]]
&];
(*Some numbers are too big for plotting. Make them numeric if they are too big*)
(*Include equations*)
Simplify@
{
{
equListC[[equ1]],
equListC[[equ2]]
},
(*Include points*)
Table[
{
poi[[$i]][[1]],
poi[[$i]][[2]],
(*Include gradients*)
{
(D[equList[[equ1]],dom])/.
dom->poi[[$i]][[1]],
(D[equList[[equ2]],dom])/.
dom->poi[[$i]][[1]]
}
},{$i,Length[poi]}
]
}
},
{equ2,equ1+1,Length[equList]}],
{equ1,Length[equList]}
],
2],
(*If equList has a length of 1 or less, return nothing*)
result={}
];
Unprotect[spdPOI];
spdPOI[Unevaluated@equList,dom,domLow,domHigh,rangeLow,rangeHigh]=result;
Protect[spdPOI];
Return[result,spdPOI]
}
]
};
(*Find vertical asymptotes*)
spdAsyV[equList_,dom_:x,domLow_:-10,domHigh_:10]:=
{
Quiet@Module[{
lines,
result
},
{
(*Remove all non numbers*)
lines=Select[
Flatten@Table[
dom/.Solve[
1/equList[[$i]][[1]]==0&&Floor@domLow<=dom<=Ceiling@domHigh
],
{$i,Length[equList]}
],
NumericQ
];
(*Convert into useable points
Caches results for spdPlot*)
result=
(*TooltipString*)
Table[{
{
"Vertical Asymptote",
{N[lines[[$i]],spdRoundPrecision],lines[[$i]]}
},
(*Points*)
{{lines[[$i]],0},{lines[[$i]],1}}
},
{$i,Length[lines]}
];
Unprotect[spdAsyV];
spdAsyV[equList,dom,domLow,domHigh]=result;
Protect[spdAsyV];
Return[result,spdAsyV]
}
]
};
(*Find other asymptotes*)
spdAsyO[equ_,dom_:x]:=
{
Module[
{
result
},
{
result=
spd`Private`spdEquPlotG[
(*Remove all the wrong asymptotes, non-asymptotes, duplicates*)
DeleteCases[
Complement[
DeleteDuplicates@Flatten@
{
(*Just find the other asymptotes*)
Map[spd`Private`$Noty/.#&,Flatten@Map[ResourceFunction["Asymptotes"][Unevaluated@equ,dom,$Noty,#]&,{"Oblique","Parabolic","Other"}]],
(*Thank you for staying quiet*)
Quiet@PolynomialQuotient[Numerator[Together@equ],Denominator[Together@equ],dom]
},
{$Noty,equ}
],
_PolynomialQuotient
],
"Asymptote",Column@{"Original Equation",Defer@equ},dom];
Unprotect[spdAsyO];
spdAsyO[Unevaluated@equ,dom]=result;
Protect[spdAsyO];
Return[result,spdAsyO]
}
]
};
(*Find tangent*)
spdFindTangent[equ_,xCoord_,dom_:x]:={
Quiet@Module[{
grad,
yCoord,
tangent,
result
},{
(*Find gradient*)
grad=D[equ,dom]/.dom->xCoord;
(*Find yCoord*)
yCoord=equ/.dom->xCoord;
(*Find tangent*)
tangent=grad(dom-xCoord)+yCoord;
If[
(*If y is indeterminate or infinity*)
And@@(FreeQ[{grad,yCoord,tangent},#]&/@{Indeterminate,ComplexInfinity,DirectedInfinity}),
result=Simplify@{
(*Convert into graphics*)
(*Create a string of data to display*)
(*TooltipString*)
{"Find Tangent",
{"Equation:",Defer@equ},
{"Tangent:",tangent},
{"Gradient:",grad}
},
{{0,tangent/.dom->0},{xCoord,yCoord}}
},
result={}];
Unprotect[spdFindTangent];
spdFindTangent[Unevaluated@equ,xCoord,dom]=result;
Protect[spdFindTangent];
Return[result,spdFindTangent]
}]
};
(*Find normal*)
spdFindNormal[equ_,xCoord_,dom_:x]:={
Quiet@Module[{
grad,
yCoord,
normal,
result
},{
(*Find gradient*)
grad=-1/D[equ,dom]/.dom->xCoord;
(*Find yCoord*)
yCoord=equ/.dom->xCoord;
(*Find normal*)
normal=grad(dom-xCoord)+yCoord;
If[
(*If y is indeterminate or infinity*)
And@@(FreeQ[{grad,yCoord,normal},#]&/@{Indeterminate,ComplexInfinity,DirectedInfinity}),
result=Simplify@{
(*Convert into graphics*)
(*Create a string of data to display*)
(*TooltipString*)
{"Find Normal",
{"Equation:",Defer@equ},
{"Tangent:",normal},
{"Gradient:",grad}
},
{{0,normal/.dom->0},{xCoord,yCoord}}
},
result={}];
Unprotect[spdFindNormal];
spdFindNormal[Unevaluated@equ,xCoord,dom]=result;
Protect[spdFindNormal];
Return[result,spdFindNormal]
}]
};
(*Find End Points*)
spdEndPoints[equ_,dom_:x,domLow_:-10,domHigh_:10,rangeLow_:-10,rangeHigh_:10]:={
Quiet@Module[{
rules=Dispatch[{Inequality->List,Or->List,Less->Nothing,LessEqual->List,Greater->Nothing,GreaterEqual->List,True->List,Null->List,Floor@domLow-1->Sequence,Ceiling@domHigh+1->Sequence}],
epX=FunctionDomain[Unevaluated@equ,dom,Reals],
epY,
edges,
result},
{
(*Find if edges exist using the solve method*)
edges=DeleteDuplicates@{
(*Left Wall*)
Map[{domLow,#,D[Unevaluated@equ,dom]/.dom->domLow}&,y/.Solve[y==Unevaluated@equ&&rangeLow<=y<=rangeHigh/.dom->domLow,y,Reals]],
(*Right Wall*)
Map[{domHigh,#,D[Unevaluated@equ,dom]/.dom->domHigh}&,y/.Solve[y==Unevaluated@equ&&rangeLow<=y<=rangeHigh/.dom->domHigh,y,Reals]],
(*Top Wall*)
Map[{#,rangeLow,D[Unevaluated@equ,dom]/.dom->#}&,dom/.Solve[rangeLow==Unevaluated@equ&&domLow<=dom<=domHigh,dom,Reals]],
(*Bottom Wall*)
Map[{#,rangeHigh,D[Unevaluated@equ,dom]/.dom->#}&,dom/.Solve[rangeHigh==Unevaluated@equ&&domLow<=dom<=domHigh,dom,Reals]]
};
(*Get every expression possible*)
epX=Level[epX,{0,\[Infinity]}];
(*Parse out everything not less or greater*)
epX=Table[Select[epX,Head@#===i&],{i,{LessEqual,GreaterEqual,Unequal}}];
(*Get only numbers from expression*)
epX=DeleteDuplicates@Select[Level[epX,{0,\[Infinity]}],NumericQ];
(*Find the y coords of each xCoord*)
epY=Table[Limit[equ,x->epX[[$i]]],{$i,Length[epX]}];
(*All accept lists that are 3 long*)
edges=Select[Flatten[edges,1],Length@#==3&];
(*Remove points if any of the 3 points are not numeric*)
edges=Select[edges,AllTrue[#,NumericQ]&];
(*Combine both methods of calculating points*)
edges=Flatten[{edges,Transpose@{epX,epY,Table["Indeterminate",Length[epX]]}},1];
result={{Defer@equ},edges};
Unprotect[spdEndPoints];
spdEndPoints[Unevaluated@equ,dom,domLow,domHigh,rangeLow,rangeHigh]=result;
Protect[spdEndPoints];
Return[result,spdEndPoints]
}]
};
(*Must be input with Unevaluated@expr*)
spdHoles[expr_,dom_:x]:={
Quiet@Module[{
holeX=FunctionDomain[Unevaluated@expr,dom,Reals],
holeY,
rules=Dispatch[{\[Infinity]->Indeterminate,-\[Infinity]->Indeterminate}],
result,
combine
},{
(*Get every expression possible*)
holeX=Level[holeX,{0,\[Infinity]}];
(*Parse out everything not less or greater*)
holeX=Table[Select[holeX,Head@#===i&],{i,{Less,Greater}}];
(*Get only numbers from expression*)
holeX=DeleteDuplicates@Select[Level[holeX,{0,\[Infinity]}],NumericQ];
(*If not a single x point is found, return nothing*)
If[Flatten@holeX=={},Return[{},spdHoles]];
(*Find the y coordinates that don't exist*)
holeY=Map[Limit[expr,dom->#]&,holeX];
(*Combine holeX and holeY*)
combine=Transpose@{holeX,holeY,Table["Its a Hole",Length[holeX]]};
(*Filter out all indeterminate and infinite points*)
combine=Select[combine/.rules,#[[2]]=!=Indeterminate&];
result={Defer@expr,combine};
Unprotect[spdHoles];
spdHoles[Unevaluated@expr,dom]=result;
Protect[spdHoles];
Return[result,spdHoles]
}]
};
(*Function for complete the square*)
spdCompleteTheSquare[a_,b_,c_,Optional[verbose_,False]]:={
If[verbose==True,
(*Declare complete the square string first*)
Module[{
cmpString={"Step 1: \!\(\*SuperscriptBox[\(ax\), \(2\)]\) + bx + c = 0",
HoldForm[x^2 + b x + c == 0],
"",
"Step 2: \!\(\*SuperscriptBox[\(x\), \(2\)]\) + \!\(\*FractionBox[\(b\), \(a\)]\)x + \!\(\*FractionBox[\(c\), \(a\)]\) = \!\(\*FractionBox[\(0\), \(a\)]\)",
HoldForm[x^2 + b/a x + c/a == 0],
"",
"Step 3: \!\(\*SuperscriptBox[\(x\), \(2\)]\) + \!\(\*FractionBox[\(b\), \(a\)]\)x = -\!\(\*FractionBox[\(c\), \(a\)]\)",
HoldForm[x^2 + b/a x == -(c/a)],
"",
"Step 4: \!\(\*SuperscriptBox[\(x\), \(2\)]\) + \!\(\*FractionBox[\(b\), \(a\)]\)x + (\!\(\*FractionBox[\(b\), \(2 a\)]\)\!\(\*SuperscriptBox[\()\), \(2\)]\) = -\!\(\*FractionBox[\(c\), \(a\)]\) + (\!\(\*FractionBox[\(b\), \(2 a\)]\)\!\(\*SuperscriptBox[\()\), \(2\)]\)",
HoldForm[x^2 + b/a x + (b/(2a))^2 == -(c/a) + (b/(2a))^2],
"",
"Step 5: (x + \!\(\*FractionBox[\(b\), \(2 a\)]\)\!\(\*SuperscriptBox[\()\), \(2\)]\) = -\!\(\*FractionBox[\(c\), \(a\)]\) + (\!\(\*FractionBox[\(b\), \(2 a\)]\)\!\(\*SuperscriptBox[\()\), \(2\)]\)",
HoldForm[(x + b/(2a))^2 == -(c/a) + (b/(2a))^2],
"",
"Step 6: a(x + \!\(\*FractionBox[\(b\), \(2 a\)]\)\!\(\*SuperscriptBox[\()\), \(2\)]\) + c - \!\(\*FractionBox[SuperscriptBox[\(b\), \(2\)], \(4 a\)]\) = 0"}}
,{
(*Create Table of Styled string, that iterates over complete the square spring*)
Print[TableForm@Table[spdStyle[spdForm@cmpString[[$i]]],{$i,Length[cmpString]}]]
}]];
a (x+b/(2a))^2+c-b^2/(4a)==0//spdForm
}[[1]];
(*-------------------------Objects-------------------------
Compute points with tooltip*)
spdPointsG[points_,label_,colour_:spdColourPoint1]:=
Graphics[Table[Table[
(*Create a string of data to display*)
DynamicModule[{
tooltipString=spdStyle@Column@{label,
(*$i hate all of these brackets with all my heart,
and $i will never be able to debug this in the future*)
TableForm@{"Equation:",points[[point]][[1]]},
TableForm@{
{"X:",N[points[[point]][[2]][[point2]][[1]],spdRoundPrecision],Rationalize[points[[point]][[2]][[point2]][[1]]]},
{"Y:",N[points[[point]][[2]][[point2]][[2]],spdRoundPrecision],Rationalize@Rationalize[points[[point]][[2]][[point2]][[2]]]},
{"Grad:",N[points[[point]][[2]][[point2]][[3]],spdRoundPrecision],Simplify[points[[point]][[2]][[point2]][[3]]]}
}
}
},{
(*Event handler can handle clicks*)
EventHandler[
(*Convert into tooltip form*)
Tooltip[
{
{PointSize[Large],Darker@colour,Point[N@{points[[point]][[2]][[point2]][[1]],points[[point]][[2]][[point2]][[2]]}]},
{PointSize[Medium],Lighter@Lighter@colour,Point[N@{points[[point]][[2]][[point2]][[1]],points[[point]][[2]][[point2]][[2]]}]}
},
(*Give coordinate when you hover over location*)
tooltipString//spdForm
],
(*If a click is detected on point, print whatever is in tooltip*)
{"MouseClicked":>Print@Framed[tooltipString,Background->LightYellow]}]
}],
(*Iterate over every point*)
{point2,Length[points[[point]][[2]]]}],
(*Iterate over every equation*)
{point,Length[points]}]];
spdPointsGPOI[points_,label_,colour_:spdColourPoint1]:=
Graphics[Table[Table[
(*Create a string of data to display*)
DynamicModule[{
tooltipString=spdStyle[Column@{label,
(*$i hate all of these brackets with all my heart,
and $i will never be able to debug this in the future*)
TableForm@{
{"Equation:",Style[points[[point]][[1]][[1]],Background->Lighter@spdColourPoint1],Style[points[[point]][[1]][[2]],Background->Lighter@spdColourPoint2]},
{"X:",N[points[[point]][[2]][[point2]][[1]],spdRoundPrecision],Rationalize@points[[point]][[2]][[point2]][[1]]},
{"Y:",N[points[[point]][[2]][[point2]][[2]],spdRoundPrecision],Rationalize@points[[point]][[2]][[point2]][[2]]},
{"Grad:",Style[Column@{N[points[[point]][[2]][[point2]][[3]][[1]],spdRoundPrecision],Simplify@points[[point]][[2]][[point2]][[3]][[1]]},Background->Lighter@spdColourPoint1],Style[Column@{N[points[[point]][[2]][[point2]][[3]][[2]],spdRoundPrecision],Simplify@points[[point]][[2]][[point2]][[3]][[2]]},Background->Lighter@spdColourPoint2]}
}
}]
},{
(*Event handler can handle clicks*)
EventHandler[
(*Convert into tooltip form*)
Tooltip[
{
{PointSize[Large],Darker@colour,Point[N@{points[[point]][[2]][[point2]][[1]],points[[point]][[2]][[point2]][[2]]}]},
{PointSize[Medium],Lighter@Lighter@colour,Point[N@{points[[point]][[2]][[point2]][[1]],points[[point]][[2]][[point2]][[2]]}]}
},
(*Give coordinate when you hover over location*)
tooltipString//spdForm
],
(*If a click is detected on point, print whatever is in tooltip*)
{"MouseClicked":>Print@Framed[tooltipString,Background->LightYellow]}]
}],
(*Iterate over every point*)
{point2,Length[points[[point]][[2]]]}],
(*Iterate over every equation*)
{point,Length[points]}]];
(*Compute lines with tooltip*)
spdLinesG[points_,colour_:spdColourLine]:=
Graphics[Table[Table[
(*Create a string of data to display*)
DynamicModule[{
tooltipString=spdStyle[TableForm@points[[point]][[1]]]
},{
(*Event handler can handle clicks*)
EventHandler[
(*Convert into tooltip form*)
(*This is disgusting*)
Tooltip[{Dashed,colour,InfiniteLine[N@points[[point]][[2]]]},
(*Give coordinate when you hover over location*)
tooltipString//spdForm
],
(*If a click is detected on point, print whatever is in tooltip*)
{"MouseClicked":>Print@Framed[tooltipString,Background->LightYellow]}]
}],
(*Iterate over every line*)
{point2,Length[points[[point]][[2]]]}],
(*Iterate over every equation*)
{point,Length[points]}]];
(*Idk why this has to be evaluated
$i made it work but its messy code*)
spdEquPlotG[equ_,message_:"Equation",message2_:"",dom_:x]:=
{Module[{head,context},
{
If[Flatten@{equ}=={},
(*If equation is empty, make it return nothing*)
spd`Private`spdEquPlotG[Unevaluated@equ,message,dom]={},