|
122 | 122 | FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]], |
123 | 123 | "**"->"^"] |
124 | 124 |
|
| 125 | +StandardizeString[str_String]:=StringReplace[ |
| 126 | + FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]], |
| 127 | + "**"->"^"] |
| 128 | + |
| 129 | +(*StandardizeExpression: a function that performs a number of standard replacements |
| 130 | +at the Expression stage, namely: |
| 131 | +- replacing s_[arg_Plus] by s*(arg) unless s is a symbol representing a known function |
| 132 | +- replacing expressions of the form dy_^n_/dx_^n_ with y'[x]^n |
| 133 | +- if the option SuppressIndependentVariable is set to True, replacing each y'[x] with y'*) |
| 134 | + |
| 135 | +Options[StandardizeExpression] = {SuppressIndependentVariable -> True}; |
| 136 | + |
| 137 | +StandardizeExpression[expr_, OptionsPattern[]]:=Module[{output,suppress}, |
| 138 | + suppress = OptionValue[SuppressIndependentVariable]; |
| 139 | + output = expr/.s_Symbol[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*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 | + |
125 | 151 | (*StructureMatchQ: a function that checks whether a user's response \ |
126 | 152 | has the same structure as a given answer template, given a set of \ |
127 | 153 | named variables.*) |
|
167 | 193 | answerTemplate2=ReplaceAll[ToExpression[answerTemplate],inertFunctionRules]; |
168 | 194 | MatchQ[response2,Patternize[answerTemplate2,namedVariables]]] |
169 | 195 |
|
170 | | -equalQStructure[answer_String, response_String, params_Association] := Module[{namedVariables,correctQ,expr}, |
| 196 | +equalQStructure[answer_String, response_String, params_Association] := Module[{namedVariables,correctQ,expr,suppress}, |
171 | 197 | Print["Evaluating Structure"]; |
| 198 | + suppress = Lookup[params,"suppress_independent_variable",True]; |
172 | 199 | namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm]; |
173 | | - expr = ToExpression[StandardizeString[answer],TraditionalForm] /.s_Symbol[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg; |
| 200 | + expr = StandardizeExpression[ToExpression[StandardizeString[answer],TraditionalForm],SuppressIndependentVariable->suppress]; |
174 | 201 | correctQ = StructureMatchQ[ |
175 | 202 | ToString[expr,InputForm], |
176 | 203 | response, |
|
221 | 248 | SemanticAndStructureMatchQ[answer_String,response_String,answerTemplate_String,namedVariables_List] := |
222 | 249 | TrueQ[SemanticMatchQ[answer,response]&&StructureMatchQ[answerTemplate,response,namedVariables]] |
223 | 250 |
|
224 | | -equalQSemantic[answer_String, response_String, params_Association] := Module[{correctQ, expr}, |
| 251 | +equalQSemantic[answer_String, response_String, params_Association] := Module[{correctQ, expr,suppress}, |
225 | 252 | Print["Evaluating Semantic"]; |
226 | | - expr = ToExpression[StandardizeString[answer],TraditionalForm]/.s_Symbol[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg; |
| 253 | + suppress = Lookup[params,"suppress_independent_variable",True]; |
| 254 | + expr = StandardizeExpression[ToExpression[StandardizeString[answer],TraditionalForm],SuppressIndependentVariable->suppress]; |
227 | 255 | correctQ = SemanticMatchQ[ |
228 | 256 | ToString[expr,InputForm], |
229 | 257 | response]; |
|
235 | 263 | ] |
236 | 264 |
|
237 | 265 | equalQSemanticAndStructure[answer_String, response_String, params_Association] := Module[{ |
238 | | - namedVariables,answerTemplate,correctQ,answerExpr}, |
| 266 | + namedVariables,answerTemplate,correctQ,answerExpr,suppress}, |
239 | 267 | Print["Evaluating SemanticAndStructure"]; |
240 | 268 | namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm]; |
241 | | - answerTemplate = Lookup[params,"answer_template",{}]; |
242 | | - answerExpr = ToExpression[StandardizeString[answer],TraditionalForm]/.s_Symbol[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg;; |
| 269 | + answerTemplate = Lookup[params,"answer_template",{}]; |
| 270 | + suppress = Lookup[params,"suppress_independent_variable",True]; |
| 271 | + answerExpr = StandardizeExpression[ToExpression[StandardizeString[answer],TraditionalForm],SuppressIndependentVariable->suppress]; |
243 | 272 | correctQ = SemanticAndStructureMatchQ[ |
244 | 273 | ToString[answerExpr,InputForm], |
245 | 274 | response, |
|
278 | 307 | SemanticAndStrictStructureMatchQ[answer_String,response_String,answerTemplate_String,namedVariables_List] := |
279 | 308 | TrueQ[SemanticMatchQ[answer,response]&&StrictStructureMatchQ[answerTemplate,response,namedVariables]] |
280 | 309 |
|
281 | | -equalQStrictStructure[answer_String, response_String, params_Association] := Module[{namedVariables,correctQ,expr}, |
| 310 | +equalQStrictStructure[answer_String, response_String, params_Association] := Module[{namedVariables,correctQ,expr,suppress}, |
282 | 311 | Print["Evaluating Structure"]; |
283 | 312 | namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm]; |
284 | | - expr = ToExpression[StandardizeString[answer],TraditionalForm]/.s_Symbol[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg; |
| 313 | + suppress = Lookup[params,"suppress_independent_variable",True]; |
| 314 | + expr = StandardizeExpression[ToExpression[StandardizeString[answer],TraditionalForm],SuppressIndependentVariable->suppress]; |
285 | 315 | correctQ = StrictStructureMatchQ[ |
286 | 316 | ToString[expr,InputForm], |
287 | 317 | response, |
|
294 | 324 | ] |
295 | 325 |
|
296 | 326 | equalQSemanticAndStrictStructure[answer_String, response_String, params_Association] := Module[{ |
297 | | - namedVariables,answerTemplate,correctQ,expr}, |
| 327 | + namedVariables,answerTemplate,correctQ,expr,suppress}, |
298 | 328 | Print["Evaluating SemanticAndStructure"]; |
299 | 329 | namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm]; |
300 | 330 | answerTemplate = Lookup[params,"answer_template",{}]; |
301 | | - expr = ToExpression[StandardizeString[answer],TraditionalForm]/.s_Symbol[arg_Plus]/;Not[MemberQ[Attributes[s], NumericFunction]] :> s*arg; |
| 331 | + suppress = Lookup[params,"suppress_independent_variable",True]; |
| 332 | + expr = StandardizeExpression[ToExpression[StandardizeString[answer],TraditionalForm],SuppressIndependentVariable->suppress]; |
302 | 333 | correctQ = SemanticAndStrictStructureMatchQ[ |
303 | 334 | ToString[expr,InputForm], |
304 | 335 | response, |
|
0 commit comments