Is it possible to evaluate some logic expression - conjuction and alternative without using br ? I know that it is possible to do it using phi and br but I am searching for other way.
Related
I have been using Latex2SymPy for a while successfully to handle all sorts of LaTeX inputs, but I have been running into troubles with a few functions. For instance:
from latex2sympy.process_latex import process_sympy
from sympy import *
inputLatex = '\\sin{-x}\\sin{-x}'
trigsimp(process_sympy(inputLatex))
sin(x)**2
That works great: trigsimp handled the simplification well. Now, if I try:
inputLatex = '\\sin{-x}'
trigsimp(process_sympy(inputLatex))
sin(-x)
Even though this is obviously correct, I expected trigsimp() to give me -sin(x) as an answer. In fact, if I run trigsimp straight from a sympy expression:
trigsimp(sin(-x))
-sin(x)
This is what I expect. Even running sin(-x) without the trigsimp() command returns me -sin(x). I checked the object type of my process_sympy('\\sin{-x}') call, and it is 'sin'.
I thought this might be something related with the way x is transformed into a Symbol type, but I then tried putting pi in the sin function.
inputLatex = '\\sin{\\pi}'
trigsimp(process_sympy(inputLatex))
sin(pi)
If I run straight sin(pi), I get 0 as an answer, with or without trigsimp().
Can any of you shed a light on this?
Although it is probably too late, you can solve that issue by calling the doit() method in the sympy expression obtained from process_sympy(inputLatex).
When calling to process_sympy, it generates an unevaluated sympy expression, not a string. This expression cannot be evaluated just by calling sympify, since it has an attribute telling not to evaluate it.
You solved the problem by converting that expression to a string and then sympifying it, but a more direct way of doing it is just by using the doit() method in the expression:
inputLatex = '\\sin{-x}'
expr = process_sympy(inputLatex)
trigsimp(expr.doit())
would do the job.
I haven't solved the whole mystery yet, but I found a dirty workaround, which is to use sympify(). So if anyone else faces the same issue, here is what I did to get it to work:
inputLatex = '\\sin{-x}'
sympify(str(process_sympy(inputLatex)))
The answer now is -sin(x), what I wanted.
As for the sin(pi) thing, the issue is that process_sympy() cannot distinguish the pi as a symbol from pi as a number. I did the test here, and type(process_sympy('\pi')) returns Symbol type, and not a number. The same solution applies here.
I'm using regular expression in AutoWikiBrowser to replace the input of several values with just one value, such as this:
|value1=4
|value2=5
|value3=6
To this:
|value={{#expr:4+5+6}}
While the correct result does show on the page, it does not look good in the code itself, so I'm trying to find a way to make it the result only (in this case value=15) but so far no luck. Can someone help me with out showing how to make this possible?
P.S. I tried the search function but didn't find a similar question.
MediaWiki parser allows the templates can be subst'ed, ie replaced by their rendering. That's also true for parser functions call.
You can subst a template prefixing the template call by subst:.
|value={{subst:#expr:4+5+6}}
Reference: Substitution on MediaWiki manual
Example: diff (the expression used is in the edit summary, the result in the diff)
Is it possible to write replacement macros for NetBeans?
I need to replace function is_active with function isActive. It seems that is not possible with short regex.
So I wonder is it possible to write such macros?
Crazy macros code....
We cannot use find properly, so I decieded to use find-selection
caret-begin
"function[^(]*_[a-z]"
selection-begin-line
find-selection
remove-selection
find
# there is no loop, so you need to repeat this lines many-many times (too many may hang your IDE)
caret-begin
find-next
caret-forward
caret-backward
delete-previous
to-upper-case
To use this macros you need to set the focus on a document and have turned on regex find option.
Warning, could spoil your code.
It is easy to obtain such rewrite in other CAS like Mathematica.
TrigReduce[Sin[x]^2]
(*1/2 (1 - Cos[2 x])*)
However, in Sympy, trigsimp with all methods tested returns sin(x)**2
trigsimp(sin(x)*sin(x),method='fu')
While dealing with a similar issue, reducing the order of sin(x)**6, I notice that sympy can reduce the order of sin(x)**n with n=2,3,4,5,... by using, rewrite, expand, and then rewrite, followed by simplify, as shown here:
expr = sin(x)**6
expr.rewrite(sin, exp).expand().rewrite(exp, sin).simplify()
this returns:
-15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 + 5/16
That works for every power similarly to what Mathematica will do.
On the other hand if you want to reduce sin(x)**2*cos(x) a similar strategy works. In that case you have to rewrite the cos and sin to exp and as before expand rewrite and simplify again as:
(sin(x)**2*cos(x)).rewrite(sin, exp).rewrite(cos, exp).expand().rewrite(exp, sin).simplify()
that returns:
cos(x)/4 - cos(3*x)/4
The full "fu" method tries many different combinations of transformations to find "the best" result.
The individual transforms used in the Fu-routines can be used to do targeted transformations. You will have to read the documentation to learn what the different functions do, but just running through the functions of the FU dictionary identifies TR8 as your workhorse here:
>>> for f in FU.keys():
... print("{}: {}".format(f, FU[f](sin(var('x'))**2)))
...
8<---
TR8 -cos(2*x)/2 + 1/2
TR1 sin(x)**2
8<---
Here is a silly way to get this job done.
trigsimp((sin(x)**2).rewrite(tan))
returns:
-cos(2*x)/2 + 1/2
also works for
trigsimp((sin(x)**3).rewrite(tan))
returns
3*sin(x)/4 - sin(3*x)/4
but not works for
trigsimp((sin(x)**2*cos(x)).rewrite(tan))
retruns
4*(-tan(x/2)**2 + 1)*cos(x/2)**6*tan(x/2)**2
I have strings in a table that contain hex values such as \ffffffc4. An example is the following:
Urz\ffffffc4\ffffff85dzenie zgodne ze standardem High Definition Audio
The following code can convert the hex into UTF8:
select chr(x'c4'::int)
which returns Ä but when I try to use a regexp_replace I get into problems. I have tried the following:
select regexp_replace(sal_input, E'\\f{6}(..)',convert(E'\\1','xyz','UTF8'),'g')
where XYZ are the various source encodings offered in 8.2 but all I get back is the hex value.
Any idea on how I could use the chr function inside regexp_replace?
Version used: PostgreSQL 8.2.15 (Greenplum Database 4.1.1.1 build 1) on x86_64-unknown-linux-gnu
Thanks in advance for the help
You are misunderstanding the order of evaluation. The 2nd argument to regexp_replace isn't a callback invoked for every substitution of '\1'.
What happens is that your convert call is evaluated first, on the literal value \1, and that result is passed to regexp_replace.
In any case, the SQL doesn't even evaluate on a modern PostgreSQL because of stricter casting rules, as '\1' isn't a valid bytea literal.
In a less ancient Pg version it might be possible to do something with regexp_split_to_table, chr and string_agg. In 8.2, I think you're going to be using a PL. I'd load PL/Perl and write a simple Perl function to do it. It's likely possible to implement in PL/PgSQL, but I suspect any implementation with the functionality available in 8.2 will be verbose and slow. I'd love to be proved wrong.