Skip to content

Commit dd818e8

Browse files
authored
Merge pull request #13 from lambda-feedback/fix/context
Fix/context
2 parents 754825b + d4203b1 commit dd818e8

4 files changed

Lines changed: 110 additions & 121 deletions

File tree

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ ENV LOG_LEVEL="DEBUG"
1717
# keeps using the toolkit-wolfram version pinned in the base image.
1818
#COPY ./.local-toolkit /opt/lambda-feedback/toolkit-wolfram
1919

20+
COPY ./utils.m /app/utils.m
2021
COPY ./evaluate.m /app/evaluate.m
2122
COPY ./preview.m /app/preview.m

evaluate.m

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
(* For new style packages see: https://mathematica.stackexchange.com/a/176489) *)
1818
(* Declare package context *)
19-
BeginPackage["evaluate`"];
19+
Get["utils.m"];
20+
BeginPackage["evaluate`", {"utils`"}];
2021

2122
EvaluationFunction[answer_, response_, params_] := Module[{result, feedback, type},
2223
type = params["type"];
@@ -34,39 +35,9 @@
3435
|>
3536
];
3637

37-
activeFunctionRules = {
38-
sin -> Sin, cos -> Cos, tan -> Tan, sec -> Sec, Cosec -> Csc, csc -> Csc, cosec -> Csc, cot -> Cot,
39-
arcsin -> ArcSin, asin -> ArcSin, arccos -> ArcCos, acos -> ArcCos, arctan -> ArcTan, atan -> ArcTan,
40-
arcsec -> ArcSec, asec -> ArcSec, ArcCosec -> ArcCsc, arccsc -> ArcCsc, acsc -> ArcCsc, acosec -> ArcCsc,
41-
arccot -> ArcCot,acot -> ArcCot,
42-
sinh -> Sinh, cosh -> Cosh, tanh -> Tanh, sech -> Sech, Cosech -> Csch, csch -> Csch, cosech -> Csch, coth -> Coth,
43-
arcsinh -> ArcSinh, asinh -> ArcSinh, arccosh -> ArcCosh, acosh -> ArcCosh, arctanh -> ArcTanh, atanh -> ArcTanh,
44-
arcsech -> ArcSech, asech -> ArcSech,
45-
ArcCsch -> ArcCsch, ArcCosech -> ArcCsch, arccsch->ArcCsch, acsch -> ArcCsch, acosech -> ArcCsch,
46-
arccoth -> ArcCoth, acoth -> ArcCoth,
47-
exp -> Exp, log -> Log, ln -> Log, sqrt -> Sqrt,
48-
pi -> Pi, e -> E, i -> I};
49-
5038
Begin["`Private`"];
5139

52-
inertFunctionRules = {
53-
Sin -> fSin, sin -> fSin, Cos -> fCos,cos->fCos, Tan -> fTan, tan -> fTan,
54-
Sec -> fSec, sec -> fSec, Csc -> fCsc, Cosec -> fCsc, csc -> fCsc, cosec -> fCsc, Cot -> fCot, cot -> fCot,
55-
ArcSin -> fArcSin, arcsin -> fArcSin, asin -> fArcSin, ArcCos -> fArcCos, arccos -> fArcCos, acos -> fArcCos,
56-
ArcTan -> fArcTan, arctan -> fArcTan, atan -> fArcTan,
57-
ArcSec -> fArcSec, arcsec -> fArcSec, asec -> fArcSec,
58-
ArcCsc -> fArcCsc, ArcCosec -> fArcCsc, arccsc -> fArcCsc, acsc -> fArcCsc, acosec -> fArcCsc,
59-
ArcCot -> fArcCot, arccot -> fArcCot, acot -> fArcCot,
60-
Sinh -> fSinh, sinh -> fSinh, Cosh -> fCosh, cosh -> fCosh, tanh -> fTanh, tanh->fTanh,
61-
Sech -> fSech, sech -> fSech, Csch -> fCsch, Cosech -> fCsch, csch -> fCsch, cosech -> fCsch, Coth -> fCoth, coth->fCoth,
62-
ArcSinh -> fArcSinh, arcsinh -> fArcSinh, asinh -> fArcSinh, ArcCosh -> fArcCosh, arccosh -> fArcCosh, acosh -> fArcCosh,
63-
ArcTanh -> fArcTanh, arctanh -> fArcTanh, atanh -> fArcTanh,
64-
ArcSech -> fArcSech, arcsech -> fArcSech, asech -> fArcSech,
65-
ArcCsch -> fArcCsch, ArcCosech -> fArcCsch, arccsch -> fArcCsch, acsch -> fArcCsch, acosech -> fArcCsch,
66-
ArcCoth -> fArcCoth, arccoth -> fArcCoth, acoth->fArcCoth,
67-
Exp -> fExp, exp -> fExp, Log -> fLog, log -> fLog, ln -> fLog,
68-
Sqrt -> fSqrt, sqrt -> fSqrt,
69-
pi -> Pi, e -> E, i -> I};
40+
(* inertFunctionRules lives in utils.m, shared with preview` *)
7041

7142
equalQNumeric[answer_, response_, params_] := Module[{tolerance, error},
7243
Print["Evaluating Equal Numeric"];
@@ -145,41 +116,7 @@
145116

146117
Depatternize[pattern_] := MapAll[DepatternizePattern, pattern]
147118

148-
(*StandardizeString: a function that automatically converts all instances
149-
of the equals sign in a string to the repeated equals sign, so that anything WL
150-
would parse as an assignment gets parsed instead as an equation, and also carries out
151-
other standard string replacements*)
152-
153-
Options[StandardizeString] = {PlusMinusSplit->True};
154-
155-
StandardizeString[str_String,OptionsPattern[]]:=Module[{output},output=StringReplace[
156-
FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]],
157-
{"**"->"^","plus_minus"->"\[PlusMinus]","minus_plus"->"\[MinusPlus]"}];
158-
If[OptionValue[PlusMinusSplit]&&StringContainsQ[output,{"\[PlusMinus]","\[MinusPlus]"}],output="{"<>StringReplace[output,{"\[PlusMinus]"->"+","\[MinusPlus]"->"-"}]<>", "<>StringReplace[output,{"\[PlusMinus]"->"-","\[MinusPlus]"->"+"}]<>"}"];output]
159-
160-
(*StandardizeExpression: a function that performs a number of standard replacements
161-
at the Expression stage, namely:
162-
- replacing s_[arg_Plus] by s*(arg) unless s is a symbol representing a known function
163-
- replacing expressions of the form dy_^n_/dx_^n_ with y'[x]^n
164-
- if the option SuppressIndependentVariable is set to True, replacing each y'[x] with y'*)
165-
166-
Options[StandardizeExpression] = {SuppressIndependentVariable -> True};
167-
168-
StandardizeExpression[expr_, OptionsPattern[]]:=Module[{output,suppress},
169-
suppress = OptionValue[SuppressIndependentVariable];
170-
output = expr/.activeFunctionRules;
171-
output = output/.(s:_Symbol)[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg;
172-
output = output/.I[arg_]:>I*arg;
173-
output = output/.{
174-
dx_^a_. dy_^b_.:>
175-
(ToExpression[StringTake[ToString[dx],{2}]]'[StringTake[ToString[dy],{2}]])^a/;
176-
StringTake[ToString[dx],{1}]=="d"&&StringTake[ToString[dy],{1}]=="d"&&a>0&&a+b==0,
177-
dx_^a_. dy_^b_.:>
178-
(ToExpression[StringTake[ToString[dy],{2}]]'[StringTake[ToString[dx],{2}]])^a/;
179-
StringTake[ToString[dx],{1}]=="d"&&StringTake[ToString[dy],{1}]=="d"&&b>0&&a+b==0};
180-
If[suppress,output=output/.Derivative[n_][y_][x_]:>Derivative[n][y]];
181-
output
182-
]
119+
(* StandardizeString and StandardizeExpression live in utils.m, shared with preview` *)
183120

184121
(* FullStandardizeString *)
185122

preview.m

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
(* For new style packages see: https://mathematica.stackexchange.com/a/176489) *)
1818
(* Declare package context *)
19-
BeginPackage["preview`"];
19+
Get["utils.m"];
20+
BeginPackage["preview`", {"utils`"}];
2021

2122
PreviewFunction[response_, params_] := Module[{
2223
latexString, wolframString, parsedResponse, isLatex,suppress,plusMinusSplit},
@@ -48,20 +49,6 @@
4849
|>
4950
];
5051

51-
activeFunctionRules = {
52-
sin -> Sin, cos -> Cos, tan -> Tan, sec -> Sec, Cosec -> Csc, csc -> Csc, cosec -> Csc, cot -> Cot,
53-
arcsin -> ArcSin, asin -> ArcSin, arccos -> ArcCos, acos -> ArcCos, arctan -> ArcTan, atan -> ArcTan,
54-
arcsec -> ArcSec, asec -> ArcSec, ArcCosec -> ArcCsc, arccsc -> ArcCsc, acsc -> ArcCsc, acosec -> ArcCsc,
55-
arccot -> ArcCot,acot -> ArcCot,
56-
sinh -> Sinh, cosh -> Cosh, tanh -> Tanh, sech -> Sech, Cosech -> Csch, csch -> Csch, cosech -> Csch, coth -> Coth,
57-
arcsinh -> ArcSinh, asinh -> ArcSinh, arccosh -> ArcCosh, acosh -> ArcCosh, arctanh -> ArcTanh, atanh -> ArcTanh,
58-
arcsech -> ArcSech, asech -> ArcSech,
59-
ArcCsch -> ArcCsch, ArcCosech -> ArcCsch, arccsch->ArcCsch, acsch -> ArcCsch, acosech -> ArcCsch,
60-
arccoth -> ArcCoth, acoth -> ArcCoth,
61-
exp -> Exp, log -> Log, ln -> Log,
62-
sqrt ->Sqrt,
63-
pi -> Pi, e -> E, i -> I};
64-
6552
Begin["`Private`"];
6653

6754
SafeToExpression[str_String, isLatex_,suppress_,plusMinusSplit_] :=
@@ -109,46 +96,8 @@
10996
"Error: Unexpected parsing result"
11097
]
11198
]
112-
113-
(*StandardizeString: a function that automatically converts all instances
114-
of the equals sign in a string to the repeated equals sign, so that anything WL
115-
would parse as an assignment gets parsed instead as an equation*)
116-
117-
Options[StandardizeString] = {PlusMinusSplit->True};
118-
119-
StandardizeString[str_String,OptionsPattern[]]:=Module[{output},
120-
output=StringReplace[
121-
FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]],
122-
{"**"->"^","plus_minus"->"\[PlusMinus]","minus_plus"->"\[MinusPlus]"}];
123-
If[OptionValue[PlusMinusSplit]&&StringContainsQ[output,{"\[PlusMinus]","\[MinusPlus]"}],
124-
output="{"<>StringReplace[output,{"\[PlusMinus]"->"+","\[MinusPlus]"->"-"}]<>", "<>StringReplace[output,{"\[PlusMinus]"->"-","\[MinusPlus]"->"+"}]<>"}"];
125-
output]
126-
127-
(*StandardizeExpression: a function that performs a number of standard replacements
128-
at the Expression stage, namely:
129-
- replacing s_[arg_Plus] by s*(arg) unless s is a symbol representing a known function
130-
- replacing expressions of the form dy_^n_/dx_^n_ with y'[x]^n
131-
- if the option SuppressIndependentVariable is set to True, replacing each y'[x] with y'*)
132-
133-
Options[StandardizeExpression] = {SuppressIndependentVariable -> True};
134-
135-
StandardizeExpression[expr_, OptionsPattern[]]:=Module[{output,suppress},
136-
suppress = OptionValue[SuppressIndependentVariable];
137-
output = expr/.activeFunctionRules;
138-
output = output/.(s:_Symbol)[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg;
139-
output = output/.I[arg_]:>I*arg;
140-
output = output/.{
141-
dx_^a_. dy_^b_.:>
142-
(ToExpression[StringTake[ToString[dx],{2}]]'[StringTake[ToString[dy],{2}]])^a/;
143-
StringTake[ToString[dx],{1}]=="d"&&StringTake[ToString[dy],{1}]=="d"&&a>0&&a+b==0,
144-
dx_^a_. dy_^b_.:>
145-
(ToExpression[StringTake[ToString[dy],{2}]]'[StringTake[ToString[dx],{2}]])^a/;
146-
StringTake[ToString[dx],{1}]=="d"&&StringTake[ToString[dy],{1}]=="d"&&b>0&&a+b==0};
147-
If[suppress,output=output/.Derivative[n_][y_][x_]:>Derivative[n][y]];
148-
output
149-
]
150-
15199

100+
(* StandardizeString and StandardizeExpression live in utils.m, shared with evaluate` *)
152101

153102
End[];
154103
EndPackage[];

utils.m

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)