I have a netcdf file with variables tas and hurs. I want to use the expr operator of the Cdo python package to calculate a new variable h. The formula of h is:
h=5/9 * (e-10), where
e = 6.112 * 10^(7.5*tas/(237.7+tas))*hurs/100
Note that python-cdo is just a wrapper of cdo so I'd suggest you using cdo first and then translate it into the python wrapper version (if you really need it...).
I think that writing exactly the same formula as input to the expr operator should work. If you check the documentation *, +, / and ^ are all valid operators and you only need to supply the input variables for the expression using the variable names from the netcdf file infile.
cdo expr,’out=(5/9)*((6.112 * 10^(7.5*tas/(237.7+tas))*hurs/100)-10);’ infile outfile
Related
I'm using MathJax to render math equations on my website. I use Python to create those math equations and I use f-strings a lot.
Currently, I have to concatenate every string because to write a square root in mathJax (as far as I can tell), you can only use sqrt{x=3}, but that's a problem because in Python f-strings use {} too.
Is there any other way to write a square root in mathJax or fraction in mathJax without using curly brackets?
An example of what I'm talking about/in other words:
Off of what I know currently, this is how you write a square root in mathJax:
$$sqrt{4}$$
But in Python when dealing with f-strings:
x = 4
equation = f'$$sqrt{x}$$'
creates
$$sqrt4$$
– which is not what I want.
Of course I see that you can just concatenate the string by doing and not do an f-string:
equation = '$$sqrt{' + x + '}$$'
However when I'm doing larger problems, and I'm making a lot of varying equations and this takes forever to do for every string.
I'm studying over parsing with python. I have user-defined instructions. So I have to specify precedence of them. I find an example here is the link
I don't understand what they do in here
precedence = (
('left','PLUS','MINUS'),
('left','TIMES','DIVIDE'),
('right','UMINUS'),
)
how python prioritizes them?
and also those too
def p_statement_assign(t):
'statement : NAME EQUALS expression'
names[t[1]] = t[3]
def p_statement_expr(t):
'statement : expression'
print(t[1])
What does it mean to write 'statement : expression' in quotation marks? How python understand and make sense of them?
I'm adding my instruction too. I will use them for drawing something in my program
F n -> go on n step
R n -> turn right n degree
L n -> Repeat the parentheses n times
COLOR f -> f: line color
PEN n -> line thickness
These instructions are read by ply and any Python function/class/module can have these strings write at the beginning of them called docstring and you can use __doc__ attribute to retrieve them. Ply cleverly uses them as annotations to define the parsing rules. The rule can be interpreted as such: statement: NAME EQUALS expression means if there is a token stream that matches the sequence first with NAME, then EQUALS sign and finally an expression, it will be reduced to a statement.
The same is for precedence variable, which is also read by ply and ply uses this variable to define precedence rule.
I recommend you read the ply documentation before using it as you need to know the basics about tokenizing and parsing before you can use a compiler construction tool like ply.
(Sorry for my poor English:)
I know that I can use eval to calculate the result of a mathematical expression.
However, this is not accurate(e.g: 0.3-0.2 != 0.1). After searching, I found I can use Fraction or some other methods to calculate.
But I didn't find out how to use these methods to directly calculate a string expression. For example, Fraction('239/3289') is correct but I can't use Fraction('(239/3289+392)/(12+993)').
Is there a simple way to accurately calculate an mathematical expression?
edit: Actually I'm just wondering how to parse such a formula and return the result in Fraction instead of splitting the string and using Fraction(Fraction('239/3289') + 392, 12 + 993)...Does someone know how to do it?
Try math.isclose and cmath.isclose as suggested here
The argument to Fraction is not an arbitrary expression, it's just for creating one fraction. You can then use that like a number in a more general expression.
Fraction(Fraction('239/3289') + 392, 12 + 992)
I have an input like "2(5x+4) + 3(2x-1)" in plain text and I need to expand and simplify it. It appears sympy requires it to be entered as a python object made up of python types. Is there a way to automatically parse it / a library that doesn't require it so I can give it the string and it gives me the answer in a human readable format?
sympy already has such a parser built-in under sympy.parsing.sympy_parser.parse_expr. To get the results you want with your input statement you also have to add the implicit_multiplication transformation (since sympy otherwise won't generate statements that make sense for 2( and 5x):
from sympy.parsing.sympy_parser import (
parse_expr,
standard_transformations,
implicit_multiplication,
)
parse_expr("2(5x+4) + 3(2x-1)", transformations=standard_transformations + (implicit_multiplication,))
You want to use sympify function, for your expression it's gonna be like this:
sympify('2*(5*x+4) + 3*(2*x-1)')
I have a Python string containing a function such as:
function = x1 + ((500 * x3) - (2 / x5))
and I want to format this function as:
add(x1, subtract(multiply(500, x3), divide (2, x5)))
What is the best way to go about doing that? Would appreciate any help since I am new to python!
Its not about Python. Its about different notations in writing expressions which, in turn, can be a different way of parsing a properly constructed tree with each leaf be a number and each node an operation.
That been said, you must construct a binary expression tree and then parse it with the "pre-fix" way. In addition, you should replace the information/symbols in nodes with the functions/strings that you want.
More, introductory, info:
https://en.wikipedia.org/wiki/Binary_expression_tree
https://en.wikipedia.org/wiki/Tree_traversal