Mathematica automatically puts pluses in list - list

I have a notebook that I have been using quite a while and everything worked perfect up until now. Somehow when evaluating the notebook mathematica puts a plus begin every value in the lists like this:
(x1 +, x2 +, x3 +, ... , xn +)
Now I can't use the lists anymore for graphical things. Yesterday I did not have this problem yet, and I changed nothing in the code so I don't know where this comes from, I hope any of you have an idea how to solve this.

Related

How do I use FileTemplate as a substitute for Splice to export Mathematica expressions into Fortran?

I found the Splice functionality in Mathematica quite useful in the past. I am trying to insert mathematica expressions, formatted for Fortran, into Fotran code.
Does anyone have a small working example they would be willing to share? Thanks.
I could write Mathematica code
y=x^3
and construct a file test.fm with
program test
real x,y
x=1.0
y=
- <* y *>
write(6,*) "y",y
end
and the mathematica line
Splice["test.fm"]
would give a file test.f with
program test
real x,y
x=1.0
y=
- x**3
write(6,*) "y",y
end
Apparently this use of Splice is removed in recent Mathematica releases, and I get an error message
The function Splice with filename inputs is now obsolete and has been
superseded by FileTemplate.
I tried
FileTemplate["test.mf"]
but it returns something that apparently needs further output. I then tried
TemplateApply[FileTemplate["my.fm"]]
but this didn't work either.
FileTemplate yields a TemplateObject expression that you need to apply. Here is an example application using your code and StringTemplate, which similarly yields a TemplateObject expression.
Clear[x]
y = x^3
StringTemplate[" program test
real x,y
x=1.0
y=
- <* y *>
write(6,*) \"y\",y
end", InsertionFunction -> ToString#*FortranForm][<|"y" -> y|>]

Console like application in CPP

I have been assigned with a coding homework in CPP where I'm supposed to create a Matrix calculator.
This would be an ok-ish task considering all I need to do is matrix calculations, use polymorphism, consider memory consumption, and some more criteria.
What I'm struggling with is that is should be a console like environment.
Meaning start the app and I'm in a prompt where I type commands like:
scan x[3][3] \n // this creates new matrix labeled 'x' and waits for 9 ints ( longs possibly ) to by typed.
z = add x y \n // or
z = x + y \n
I am familiar with automata theory ( to some degree ) and making it this simple shouldn't be a problem.
( that practically has nothing to do with automata )
Simple meaning one command per line - because that's what I'm doing now.
I have some parser class that breaks down the command, and than I do the necessary changes. Its more of a if-else tree going from first word to the last. If I encounter unrecognized word - Grammar/Syntax error.
What I'm asking is some tips on how to make it more .. bash like, for instance.
Since the app is run in bash..
FIRST Q: how do i achieve a history of typed commands ? rn when i push arrowup i get those ^[[A.
SECOND Q: Some hints how start parsing some more complicated commands like: a = b = c * ( d + q ) ( implying that 'c' can multiply "(d + q)" and 'd' is addable to 'q' etc.. ) bcs that cant be done with the static way my parser works rn.
Thank you all.
For your first question, look at libreadline or libeditline. If you do not want to lift a finger, run your program under the rlwrap wrapper.
For your second question, look at Simple library or implementation for a mathematical expression evaluator . Look for one that allows you to define and use variables.

Parsing series of formulas from a string using sympy

I have a pandas df that contains many string formulas that I would like to be able to parse and eventually solve. I came across parse_expr and initially seemed like it would work for my problem but now I'm not so sure.
An example string formula might look like this:
A = B + C; D = A*.2;
parse_expr would seem to work well if i had a system of equations and I may not be using this correctly. As it stands, parse_expr throws an "invalid syntax" error I believe because of the equal sign. Can anyone tell if its possible to solve this problem using parse_expr or if there is another approach I should try?
SymPy cannot parse a bunch of semicolon-separated formulas at once, so the string needs to be split first. It will need to be split again at =, assuming all formulas have = in them. After parsing each side of =, you can combine them with Eq, which is SymPy's equation object; or use them somehow else.
from sympy import S, Eq
str = "A = B + C; D = A*.2;"
result = [Eq(*map(S, f.split("="))) for f in str.split(";")[:-1]]
The result is [Eq(A, B + C), Eq(D, 0.2*A)]
I use S, short for sympify; parse_expr could be used similarly, and it has a few options that are not needed here.
parse_expr is based on the Python tokenizer, but it has several extensions. These extensions take the form of functions that take a list of tokens, a locals dictionary, and a globals dictionary, and return a modified list of tokens. These are passed as a tuple to parse_expr, like parse_expr(expression, transformations=(transformation1, transformation2, ...)).
It's probably easiest to just take a look at the source of the sympy.parsing.sympy_parser submodule to see the existing transformations and how they work. Some of the transformations that are there will probably be useful to you. In this case, you would want a transformation that transforms the = token into something else (actually there's already a transformation function convert_equals_sign in the sympy_parser submodule that does this). You assumedly also want to handle *. somehow.
I've also written a guide on Python tokenization which may be helpful here: https://www.asmeurer.com/brown-water-python
If your syntax is too far off from Python's then it will be challenging to use parse_expr, since it only works with Python's tokenizer. In that case, you'd need to generate your own grammar and parser (e.g., using antlr) for your DSL and parse it into something that can then be transformed into a SymPy expression.

How to rewrite `sin(x)^2` to cos(2*x) form in Sympy

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

express router - regex - semicolon separated list

i' ve an express router which i want to accept a semicolon separated list. * should stand for 0 or more values, however it accepts only one or more in my case.
Here is my code:
App.get('/sth/((\\w+(\;\\w+)*))',
however it accepts only
/sth/aaa;bbb
/stg/aaa;bbb;ccc
/sth/aaa;bbb;ccc;ddd
...
, but not
/sth/aaa
.
How can i achieve my goal or what' s wrong with my regexp? Probably i miss just one trivial thing.
Thanks.
Change it to:
/sth/\\w+(;\\w+)*
A workaround or a solution would be something similar
App.get('/sth/((\\w+(;\\w+){0,}))',
As I experienced, express doesn't use the standard regexp, but it has its own implementation, and the * has a different usecase. It' d be nice to know how it' s treated, but to me it seems it gets everything from 1 to infinity.