Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
75b56126
Commit
75b56126
authored
Mar 14, 2013
by
Peter Baratta
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Script feature fix
parent
49f85211
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
157 additions
and
3 deletions
+157
-3
doc/public/course_data_formats/symbolic_response.rst
+17
-3
lms/lib/symmath/formula.py
+25
-0
lms/lib/symmath/test_formula.py
+115
-0
No files found.
doc/public/course_data_formats/symbolic_response.rst
View file @
75b56126
...
@@ -19,8 +19,22 @@ This is a partial list of features, to be revised as we go along:
...
@@ -19,8 +19,22 @@ This is a partial list of features, to be revised as we go along:
An example of a problem::
An example of a problem::
<symbolicresponse expect="a_b^c + b_x__d" size="30">
<symbolicresponse expect="a_b^c + b_x__d" size="30">
<textline math="1" preprocessorClassName="SymbolicMathjaxPreprocessor" preprocessorSrc="/static/js/capa/symbolic_mathjax_preprocessor.js"/>
<textline math="1"
</symbolicresponse>
preprocessorClassName="SymbolicMathjaxPreprocessor"
preprocessorSrc="/static/js/capa/symbolic_mathjax_preprocessor.js"/>
</symbolicresponse>
It's a bit of a pain to enter that.
It's a bit of a pain to enter that.
* The script-style math variant. What would be outputted in latex if you
entered ``\mathcal{N}``. This is used in some variables.
An example::
<symbolicresponse expect="scriptN_B + x" size="30">
<textline math="1"/>
</symbolicresponse>
There is no fancy preprocessing needed, but if you had superscripts or
something, you would need to include that part.
lms/lib/symmath/formula.py
View file @
75b56126
...
@@ -74,6 +74,15 @@ def to_latex(x):
...
@@ -74,6 +74,15 @@ def to_latex(x):
# LatexPrinter._print_dot = _print_dot
# LatexPrinter._print_dot = _print_dot
xs
=
latex
(
x
)
xs
=
latex
(
x
)
xs
=
xs
.
replace
(
r'\XI'
,
'XI'
)
# workaround for strange greek
xs
=
xs
.
replace
(
r'\XI'
,
'XI'
)
# workaround for strange greek
# substitute back into latex form for scripts
# literally something of the form
# 'scriptN' becomes '\\mathcal{N}'
# note: can't use something akin to the _print_hat method above because we sometimes get 'script(N)__B' or more complicated terms
xs
=
re
.
sub
(
r'script([a-zA-Z0-9]+)'
,
'
\\
mathcal{
\\
1}'
,
xs
)
#return '<math>%s{}{}</math>' % (xs[1:-1])
#return '<math>%s{}{}</math>' % (xs[1:-1])
if
xs
[
0
]
==
'$'
:
if
xs
[
0
]
==
'$'
:
return
'[mathjax]
%
s[/mathjax]<br>'
%
(
xs
[
1
:
-
1
])
# for sympy v6
return
'[mathjax]
%
s[/mathjax]<br>'
%
(
xs
[
1
:
-
1
])
# for sympy v6
...
@@ -106,6 +115,7 @@ def my_sympify(expr, normphase=False, matrix=False, abcsym=False, do_qubit=False
...
@@ -106,6 +115,7 @@ def my_sympify(expr, normphase=False, matrix=False, abcsym=False, do_qubit=False
'i'
:
sympy
.
I
,
# lowercase i is also sqrt(-1)
'i'
:
sympy
.
I
,
# lowercase i is also sqrt(-1)
'Q'
:
sympy
.
Symbol
(
'Q'
),
# otherwise it is a sympy "ask key"
'Q'
:
sympy
.
Symbol
(
'Q'
),
# otherwise it is a sympy "ask key"
'I'
:
sympy
.
Symbol
(
'I'
),
# otherwise it is sqrt(-1)
'I'
:
sympy
.
Symbol
(
'I'
),
# otherwise it is sqrt(-1)
'N'
:
sympy
.
Symbol
(
'N'
),
# or it is some kind of sympy function
#'X':sympy.sympify('Matrix([[0,1],[1,0]])'),
#'X':sympy.sympify('Matrix([[0,1],[1,0]])'),
#'Y':sympy.sympify('Matrix([[0,-I],[I,0]])'),
#'Y':sympy.sympify('Matrix([[0,-I],[I,0]])'),
#'Z':sympy.sympify('Matrix([[1,0],[0,-1]])'),
#'Z':sympy.sympify('Matrix([[1,0],[0,-1]])'),
...
@@ -266,6 +276,21 @@ class formula(object):
...
@@ -266,6 +276,21 @@ class formula(object):
elif
tag
==
'mrow'
:
return
''
.
join
([
flatten_pmathml
(
y
)
for
y
in
xml
])
elif
tag
==
'mrow'
:
return
''
.
join
([
flatten_pmathml
(
y
)
for
y
in
xml
])
raise
Exception
,
'[flatten_pmathml] unknown tag
%
s'
%
tag
raise
Exception
,
'[flatten_pmathml] unknown tag
%
s'
%
tag
def
fix_mathvariant
(
parent
):
'''Fix certain kinds of math variants
Literally replace <mstyle mathvariant="script"><mi>N</mi></mstyle>
with 'scriptN'. There have been problems using script_N or script(N)
'''
for
child
in
parent
:
if
(
gettag
(
child
)
==
'mstyle'
and
child
.
get
(
'mathvariant'
)
==
'script'
):
newchild
=
etree
.
Element
(
'mi'
)
newchild
.
text
=
'script
%
s'
%
flatten_pmathml
(
child
[
0
])
parent
.
replace
(
child
,
newchild
)
fix_mathvariant
(
child
)
fix_mathvariant
(
xml
)
# find "tagged" superscripts
# find "tagged" superscripts
# they have the character \u200b in the superscript
# they have the character \u200b in the superscript
# replace them with a__b so snuggle doesn't get confused
# replace them with a__b so snuggle doesn't get confused
...
...
lms/lib/symmath/test_formula.py
0 → 100644
View file @
75b56126
"""
Tests of symbolic math
"""
import
unittest
import
formula
import
re
from
lxml
import
etree
def
stripXML
(
xml
):
xml
=
xml
.
replace
(
'
\n
'
,
''
)
xml
=
re
.
sub
(
r'\> +\<'
,
'><'
,
xml
)
return
xml
class
FormulaTest
(
unittest
.
TestCase
):
# for readability later
mathml_start
=
'<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle displaystyle="true">'
mathml_end
=
'</mstyle></math>'
def
setUp
(
self
):
self
.
formulaInstance
=
formula
.
formula
(
''
)
def
test_replace_mathvariants
(
self
):
expr
=
'''
<mstyle mathvariant="script">
<mi>N</mi>
</mstyle>'''
expected
=
'<mi>scriptN</mi>'
# wrap
expr
=
stripXML
(
self
.
mathml_start
+
expr
+
self
.
mathml_end
)
expected
=
stripXML
(
self
.
mathml_start
+
expected
+
self
.
mathml_end
)
# process the expression
xml
=
etree
.
fromstring
(
expr
)
xml
=
self
.
formulaInstance
.
preprocess_pmathml
(
xml
)
test
=
etree
.
tostring
(
xml
)
# success?
self
.
assertEqual
(
test
,
expected
)
def
test_fix_simple_superscripts
(
self
):
expr
=
'''
<msup>
<mi>a</mi>
<mrow>
<mo>​</mo>
<mi>b</mi>
</mrow>
</msup>'''
expected
=
'<mi>a__b</mi>'
# wrap
expr
=
stripXML
(
self
.
mathml_start
+
expr
+
self
.
mathml_end
)
expected
=
stripXML
(
self
.
mathml_start
+
expected
+
self
.
mathml_end
)
# process the expression
xml
=
etree
.
fromstring
(
expr
)
xml
=
self
.
formulaInstance
.
preprocess_pmathml
(
xml
)
test
=
etree
.
tostring
(
xml
)
# success?
self
.
assertEqual
(
test
,
expected
)
def
test_fix_complex_superscripts
(
self
):
expr
=
'''
<msubsup>
<mi>a</mi>
<mi>b</mi>
<mrow>
<mo>​</mo>
<mi>c</mi>
</mrow>
</msubsup>'''
expected
=
'<mi>a_b__c</mi>'
# wrap
expr
=
stripXML
(
self
.
mathml_start
+
expr
+
self
.
mathml_end
)
expected
=
stripXML
(
self
.
mathml_start
+
expected
+
self
.
mathml_end
)
# process the expression
xml
=
etree
.
fromstring
(
expr
)
xml
=
self
.
formulaInstance
.
preprocess_pmathml
(
xml
)
test
=
etree
.
tostring
(
xml
)
# success?
self
.
assertEqual
(
test
,
expected
)
def
test_fix_msubsup
(
self
):
expr
=
'''
<msubsup>
<mi>a</mi>
<mi>b</mi>
<mi>c</mi>
</msubsup>'''
expected
=
'<msup><mi>a_b</mi><mi>c</mi></msup>'
# which is (a_b)^c
# wrap
expr
=
stripXML
(
self
.
mathml_start
+
expr
+
self
.
mathml_end
)
expected
=
stripXML
(
self
.
mathml_start
+
expected
+
self
.
mathml_end
)
# process the expression
xml
=
etree
.
fromstring
(
expr
)
xml
=
self
.
formulaInstance
.
preprocess_pmathml
(
xml
)
test
=
etree
.
tostring
(
xml
)
# success?
self
.
assertEqual
(
test
,
expected
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment