|
| 1 | +(* ::Package:: *) |
| 2 | + |
| 3 | +(* Wolfram Language Package *) |
| 4 | + |
| 5 | +(* :Title: utils *) |
| 6 | +(* :Context: utils` *) |
| 7 | +(* :Author: marcus *) |
| 8 | +(* :Date: 2026-08-26 *) |
| 9 | + |
| 10 | +(* :Package Version: 0.1 *) |
| 11 | +(* :Mathematica Version: 14.0 *) |
| 12 | +(* :Copyright: (c) 2025 Lambda Feedback *) |
| 13 | +(* :Keywords: *) |
| 14 | + |
| 15 | +(* Shared symbol table and string/expression standardization helpers used by |
| 16 | +both evaluate` and preview`. Previously evaluate.m and preview.m each declared |
| 17 | +their own copy of activeFunctionRules/StandardizeString/StandardizeExpression; |
| 18 | +since BeginPackage narrows $ContextPath to just the new package context while |
| 19 | +its body evaluates, each copy created its own distinct symbols (e.g. |
| 20 | +evaluate`pi vs preview`pi) rather than sharing one. Whichever package loaded |
| 21 | +last "won" for any bare name typed by a user (pi, sin, e, ...), so rules like |
| 22 | +pi -> Pi could silently fail to fire depending on load order. Both packages |
| 23 | +now list this context as a BeginPackage dependency |
| 24 | +(BeginPackage["evaluate`", {"utils`"}] / BeginPackage["preview`", {"utils`"}]) |
| 25 | +so there is exactly one copy of each symbol. *) |
| 26 | + |
| 27 | +BeginPackage["utils`"]; |
| 28 | + |
| 29 | +activeFunctionRules = { |
| 30 | + sin -> Sin, cos -> Cos, tan -> Tan, sec -> Sec, Cosec -> Csc, csc -> Csc, cosec -> Csc, cot -> Cot, |
| 31 | + arcsin -> ArcSin, asin -> ArcSin, arccos -> ArcCos, acos -> ArcCos, arctan -> ArcTan, atan -> ArcTan, |
| 32 | + arcsec -> ArcSec, asec -> ArcSec, ArcCosec -> ArcCsc, arccsc -> ArcCsc, acsc -> ArcCsc, acosec -> ArcCsc, |
| 33 | + arccot -> ArcCot,acot -> ArcCot, |
| 34 | + sinh -> Sinh, cosh -> Cosh, tanh -> Tanh, sech -> Sech, Cosech -> Csch, csch -> Csch, cosech -> Csch, coth -> Coth, |
| 35 | + arcsinh -> ArcSinh, asinh -> ArcSinh, arccosh -> ArcCosh, acosh -> ArcCosh, arctanh -> ArcTanh, atanh -> ArcTanh, |
| 36 | + arcsech -> ArcSech, asech -> ArcSech, |
| 37 | + ArcCsch -> ArcCsch, ArcCosech -> ArcCsch, arccsch->ArcCsch, acsch -> ArcCsch, acosech -> ArcCsch, |
| 38 | + arccoth -> ArcCoth, acoth -> ArcCoth, |
| 39 | + exp -> Exp, log -> Log, ln -> Log, sqrt -> Sqrt, |
| 40 | + pi -> Pi, e -> E, i -> I}; |
| 41 | + |
| 42 | +inertFunctionRules = { |
| 43 | + Sin -> fSin, sin -> fSin, Cos -> fCos,cos->fCos, Tan -> fTan, tan -> fTan, |
| 44 | + Sec -> fSec, sec -> fSec, Csc -> fCsc, Cosec -> fCsc, csc -> fCsc, cosec -> fCsc, Cot -> fCot, cot -> fCot, |
| 45 | + ArcSin -> fArcSin, arcsin -> fArcSin, asin -> fArcSin, ArcCos -> fArcCos, arccos -> fArcCos, acos -> fArcCos, |
| 46 | + ArcTan -> fArcTan, arctan -> fArcTan, atan -> fArcTan, |
| 47 | + ArcSec -> fArcSec, arcsec -> fArcSec, asec -> fArcSec, |
| 48 | + ArcCsc -> fArcCsc, ArcCosec -> fArcCsc, arccsc -> fArcCsc, acsc -> fArcCsc, acosec -> fArcCsc, |
| 49 | + ArcCot -> fArcCot, arccot -> fArcCot, acot -> fArcCot, |
| 50 | + Sinh -> fSinh, sinh -> fSinh, Cosh -> fCosh, cosh -> fCosh, tanh -> fTanh, tanh->fTanh, |
| 51 | + Sech -> fSech, sech -> fSech, Csch -> fCsch, Cosech -> fCsch, csch -> fCsch, cosech -> fCsch, Coth -> fCoth, coth->fCoth, |
| 52 | + ArcSinh -> fArcSinh, arcsinh -> fArcSinh, asinh -> fArcSinh, ArcCosh -> fArcCosh, arccosh -> fArcCosh, acosh -> fArcCosh, |
| 53 | + ArcTanh -> fArcTanh, arctanh -> fArcTanh, atanh -> fArcTanh, |
| 54 | + ArcSech -> fArcSech, arcsech -> fArcSech, asech -> fArcSech, |
| 55 | + ArcCsch -> fArcCsch, ArcCosech -> fArcCsch, arccsch -> fArcCsch, acsch -> fArcCsch, acosech -> fArcCsch, |
| 56 | + ArcCoth -> fArcCoth, arccoth -> fArcCoth, acoth->fArcCoth, |
| 57 | + Exp -> fExp, exp -> fExp, Log -> fLog, log -> fLog, ln -> fLog, |
| 58 | + Sqrt -> fSqrt, sqrt -> fSqrt, |
| 59 | + pi -> Pi, e -> E, i -> I}; |
| 60 | + |
| 61 | +Options[StandardizeString] = {PlusMinusSplit->True}; |
| 62 | + |
| 63 | +Options[StandardizeExpression] = {SuppressIndependentVariable -> True}; |
| 64 | + |
| 65 | +Begin["`Private`"]; |
| 66 | + |
| 67 | +(*StandardizeString: a function that automatically converts all instances |
| 68 | +of the equals sign in a string to the repeated equals sign, so that anything WL |
| 69 | +would parse as an assignment gets parsed instead as an equation, and also carries out |
| 70 | +other standard string replacements*) |
| 71 | + |
| 72 | +StandardizeString[str_String,OptionsPattern[]]:=Module[{output}, |
| 73 | +output=StringReplace[ |
| 74 | + FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]], |
| 75 | + {"**"->"^","plus_minus"->"\[PlusMinus]","minus_plus"->"\[MinusPlus]"}]; |
| 76 | +If[OptionValue[PlusMinusSplit]&&StringContainsQ[output,{"\[PlusMinus]","\[MinusPlus]"}],output="{"<>StringReplace[output,{"\[PlusMinus]"->"+","\[MinusPlus]"->"-"}]<>", "<>StringReplace[output,{"\[PlusMinus]"->"-","\[MinusPlus]"->"+"}]<>"}"]; |
| 77 | +output] |
| 78 | + |
| 79 | +(*StandardizeExpression: a function that performs a number of standard replacements |
| 80 | +at the Expression stage, namely: |
| 81 | +- replacing s_[arg_Plus] by s*(arg) unless s is a symbol representing a known function |
| 82 | +- replacing expressions of the form dy_^n_/dx_^n_ with y'[x]^n |
| 83 | +- if the option SuppressIndependentVariable is set to True, replacing each y'[x] with y'*) |
| 84 | + |
| 85 | +StandardizeExpression[expr_, OptionsPattern[]]:=Module[{output,suppress}, |
| 86 | + suppress = OptionValue[SuppressIndependentVariable]; |
| 87 | + output = expr/.activeFunctionRules; |
| 88 | + output = output/.(s:_Symbol)[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg; |
| 89 | + output = output/.I[arg_]:>I*arg; |
| 90 | + output = output/.{ |
| 91 | + dx_^a_. dy_^b_.:> |
| 92 | + (ToExpression[StringTake[ToString[dx],{2}]]'[StringTake[ToString[dy],{2}]])^a/; |
| 93 | + StringTake[ToString[dx],{1}]=="d"&&StringTake[ToString[dy],{1}]=="d"&&a>0&&a+b==0, |
| 94 | + dx_^a_. dy_^b_.:> |
| 95 | + (ToExpression[StringTake[ToString[dy],{2}]]'[StringTake[ToString[dx],{2}]])^a/; |
| 96 | + StringTake[ToString[dx],{1}]=="d"&&StringTake[ToString[dy],{1}]=="d"&&b>0&&a+b==0}; |
| 97 | + If[suppress,output=output/.Derivative[n_][y_][x_]:>Derivative[n][y]]; |
| 98 | + output |
| 99 | +] |
| 100 | + |
| 101 | +End[]; |
| 102 | +EndPackage[]; |
0 commit comments