MathCAD XML Constructor (pyfacade.transxml)
1. Module Introduction
Module pyfacade.transxml provides some utility functions for translating mathematical expressions to
Extensible Markup Language (XML) in specific form recognized by MathCAD.
- pyfacade.transxml.xml_define(var_name, expression, ev=False, unit=None)
Translate a variable definition expression to MathCAD XML.
- Parameters:
var_name – str, name of variable.
expression – str, math expression to be assigned to variable.
ev – bool, evaluate the result of expression in line.
unit – str, unit to be used when present the result, only valid when ev = True.
- Returns:
str, formatted MathCAD XML.
- pyfacade.transxml.xml_eval(var_expression, unit=None)
Translate and evaluate a math expression to MathCAD XML.
- Parameters:
var_expression – str, math expression to be evaluated
unit – str, unit to be used when presenting the evaluated result.
- Returns:
str, formatted MathCAD XML.
- pyfacade.transxml.xml_ind(content)
Translate a individual expression or variable to MathCAD XML.
- Parameters:
content – str, math expression or variable name.
- Returns:
str, formatted MathCAD XML.
- pyfacade.transxml.xml_func(func_name, vars, expression)
Translate a function definition expression to MathCAD XML.
- Parameters:
func_name – str, name of function.
vars – str or list of str, arguments of the function.
expression – str, expression of the function.
- Returns:
str, formatted MathCAD XML.
- pyfacade.transxml.xml_sub(math_expression, subdict, simp=True)
Substitute variables in provided expression according to specified dict.
- Parameters:
math_expression – str, original expression.
subdict – dict, a mapping dict for substitution in the form of {var1: value1, var2: value2…}
simp – bool, simplify the expression by removing any item equals to 0 after substitution.
- Returns:
str, new expression with specified variables substituted
- pyfacade.transxml.to_matrix(input_array)
Produce a statement block of matrix according to input array.
- Parameters:
input_array – array_like as matrix data.
- Returns:
str, pre-formatted XML paragraph.
- pyfacade.transxml.xml_prog(statements)
Produce a statement block of in-line programing according to input string.
- Parameters:
statements – list of str, programing statements. each str represents a line of statement in programing.
- Returns:
str, pre-formatted XML paragraph.
Note
Paragraph returned by to_matrix and xml_prog can’t be directly inserted to MAthCAD XML file as elements.
Instead, they play roles like normal string of expression, which need to be translated by calling xml_define,
xml_eval or xml_ind.
Alternatively, Class Xexpr is provided as quick access working with math expression string.
- class pyfacade.transxml.Xexpr(expression, alias=None)
Formatted string of math expression.
- Parameters:
expression – str, original math expression. Invalid blank in string will be removed automatically.
alias – alias of the expression.
- __bool__()
Return False when instance represents a empty expression (‘’) or ‘0’.
- property ex
Math expression string.
- sub(subdict, inplace=True, simp=False)
Substitute variables in expression according to specified dict.
- Parameters:
subdict – dict, a mapping dict for substitution in the form of {var1: value1, var2: value2…}
inplace – bool, overwrite the expression.
simp – bool, simplify the expression by removing any item equals to 0 after substitution.
- Returns:
A new
Xexprobject with variables substituted if inplace = False. Else, return None.
- zero(vars, inplace=True, simp=True)
Set the specified variables in expression to Zero.
- Parameters:
vars – str or list of str, variable name to be set to 0.
inplace – bool, overwrite the expression.
simp – bool, simplify the expression by removing any item equals to 0 after substitution.
- Returns:
A new
XexprObject with the variables set to 0 if inplace = False. Else, return None.
- inject(dataframe, alias_col='main', expr_col='expression', alias=None)
Insert the math expression to specified data frame by matching the alias.
- Parameters:
dataframe – pandas.DataFrame, target data frame to write expression in.
alias_col – str, column in data frame where to match the alias.
expr_col – str, target column in data frame to insert expression in
alias – str, alias to be matched.
- Returns:
None
2. Syntax of Math Expression
String can be operated and translated by this module must comply with syntax stated in this section.
2.1. Plain Expression
2.1.1. Basic
A string of plain expression contains number, variable, function, or some of formers concatenated by recognized math symbols. Below are some samples of valid plain expression string:
'12' # simply a number
'5*X+Y' # number and variables concatenated by math symbols
'(sin(0.5*@p)-m^2)//2' # expression contains function and escaped character
Here are basic rules to write plain expression:
No blank in string allowed.
Always use Parentheses
'()'instead of Square Bracket'[]'or Curly Braces'{}'.A item with Negative Sign
'-'after any math symbols must be surrounded by parentheses.To input variable with subscription, add a Dot
'.'between variable name and its subscription. To input Multi-subscriptions, separate them by Dots.To input index of a array, add a Underline
'_'between variable name and the index. To input indices of multi-dimensional matrix , separate them by Underlines.To input greek letter, escape the corresponding roman letter with initial symbol
'@'. Refer to Correspondence Table.
2.1.2. Symbols and Keywords
Below lists math symbols supported by this module.
Symbol |
Meaning |
|---|---|
+ |
plus |
- |
minus |
* |
multiplication in default style |
** |
multiplication shown as ‘×’ |
// |
division |
^ |
power |
# |
n-th Root |
! |
factorial |
\\ |
square root |
| |
absolute value |
== |
equal to |
< |
less than |
<= |
less than or equal to |
> |
greater than |
>= |
greater than or equal to |
!= |
not equal to |
Note
As unary operators, '\\' and '|' should be followed by number or expression surrounded with parentheses to indicate its application extent.
Keywords for advanced math operators is shown below.
Keyword |
Equivalent Math Operator |
|---|---|
sigma (x,f(x),a,b) |
\(\sum_{a}^{b} f(x)\) |
iterp (x,f(x),a,b) |
\(\prod_{a}^{b} f(x)\) |
integral (x,f(x),a,b) |
\(\int_{a}^{b} f(x)\) |
matrix (x,y,val_1,val_2,val_3,…) |
A matrix of x rows and y columns, and filled with val_1, val_2, val_3…
in row-order.
Positions without value provided are left to 0.
|
determ (M) |
Determinate of matrix M, \(\left | M \right |\) |
transpose (M) |
Transpose of matrix M, \(M^{T}\) |
crossp (M1,M2) |
Cross product of matrix M1 and M2, \(M1\times M2\) |
The Greek and Roman Characters Correspondence Table
Greek Letter |
Roman Letter |
||
|---|---|---|---|
upper |
lower |
upper |
lower |
Α |
α |
A |
a |
Β |
β |
B |
b |
Γ |
γ |
G |
g |
Δ |
δ |
D |
d |
Ε |
ε |
E |
e |
Ζ |
ζ |
Z |
z |
Η |
η |
H |
h |
Θ |
θ |
Q |
q |
Ι |
ι |
I |
i |
Κ |
κ |
K |
k |
Λ |
λ |
L |
l |
Μ |
μ |
M |
m |
Ν |
ν |
N |
n |
Ξ |
ξ |
X |
x |
Ο |
ο |
O |
o |
Π |
π |
P |
p |
Ρ |
ρ |
R |
r |
Σ |
σ |
S |
s |
Τ |
τ |
T |
t |
Υ |
υ |
U |
u |
Φ |
φ |
F |
f |
Χ |
χ |
C |
c |
Ψ |
ψ |
Y |
y |
Ω |
ω |
W |
w |
2.1.3. Functions
Functions, either built-in functions of MathCAD or custom-defined functions, can be included in math expression by simply
state the function name followed with parentheses, in which necessary arguments is listed and separated by comma, such like
'tan(x+2)', 'max(10,15)', 'myfunc(a,b,c)'.
Warning
The validity of functions in expression will NOT be checked when being translated into XML for MathCAD. It is no problem to insert expression contains undefined function or function with improper number of arguments into MathCAD, as long as its syntax is correct.
2.2. Programming Block
Programming block can be constructed by transxml.xml_prog according to a group of statements using programming
keywords listed as below.
Operation |
Keyword |
Translated Code in MathCAD |
|---|---|---|
IF Statement |
pgif (condition,statement) |
statement if condition |
ELSE Statement |
pgelse (statement) |
statement otherwise |
FOR loop |
pgfor (i,a,s,b,statement) |
for i ∈ a,s..b
statement
|
WHILE loop |
pgwhile (condition,statement) |
while condition
statement
|
Local DEFINE statement |
locd (v,expression) |
v ← expression |
BREAK statement |
pgbreak |
break |
CONTINUE statement |
pgcontinue |
continue |
RETURN action |
pgreturn (expression) |
return expression |
Logical AND |
lgand (bool1,bool2) |
bool1 ˄ bool2 |
Logical OR |
lgor (bool1,bool2) |
bool1 ˅ bool2 |
Logical NOT |
lgnot (bool) |
¬ bool |
Logical COMPARING |
lgcp (sign,a,b) [*] |
a sign b |
Statements can be nested to build a complex programming block.