Create a multicharacter SymPy symbol for deltax using Greek - sympy

I would like to do something like deltax=symbols(",,\delta x")
But this seems to give a tuple not a multicharacter symbol. Is it even possible?
I should add that I am using Jupyter.

The function symbols is convenient in that it allows us to create several symbols at once, like a, b, c = symbols("a,b,c") or a, b, c = symbols("a b c"), or syms = symbols("a1:6"). The downside is that we can't use it to create a symbol with a comma (or a space, or a colon) in its name. Instead, the constructor of Symbol class has to be used directly:
deltax = Symbol(",,\delta x")

Are unicode identifiers in python bad practice?
Contains the answer.
In summary, using Jupyter, \Delta is followed by tab (not space), then x.

Related

What are the uses of symbols like '$ % &' in EDN?

I am new to EDN and going through EDN spec - https://github.com/edn-format/edn
What is the use of EDN symbols like '$ % &' and how can I make use of them while reading EDN in Clojure?
The spec mentions Symbols, but they don't mean it as what say on a keyboard is referred to as symbols.
Its a bit confusing, so let me re-frame it. On a keyboard for example, someone might say that there are symbols, and those would be #, #, $, %, ^, &, among others. Lets call these character symbols.
Now in EDN, you have Symbols, but it's not the referring to a character symbol. It refers to a data-type. What's even more confusing, is that it mentions that an EDN Symbol can contain a certain set of character symbol, but it is not a character symbol.
So what are EDN symbols? Here's some:
hello
abc
+
person/name
this$is#insane
Each of these is a valid EDN Symbol. It helps to contrast them to understand them. so here are a bunch of EDN Strings:
"hello"
"abc"
"+"
"person/name"
"this$is#insane"
And here's a bunch of EDN keywords:
:hello
:abc
:+
:person/name
:this$is#insane
So what distinguishes these? Well you see, EDN Symbols, Strings and Keywords are all just a set of characters, depending if it is a Symbol, String or Keyword, the allowed characters differ, and for example, that's why the EDN spec says that a Symbol can contain certain characters like $ and ?. But it does not mention all characters, for example: ^ is not allowed in a Symbol, but it is in a String:
hello^john ; Not a valid EDN symbol
"hello^john" ; A valid EDN string
What else, you can see that an EDN string must have the set of characters enclosed between open and closing double quotes "". On the other hand, a keyword must have the set of characters starting with a colon :. And a symbol doesn't need any marker, any continuous set of valid characters are a symbol, as long as they don't begin with : or are enclosed in double quotes.
Now the second thing to understand is... what are they for? This is more nebulous. They are for whatever you want to use them for when you model your data as EDN. You could use EDN strings instead, or EDN keyword instead, and vice versa. Anytime you have a set of characters that only contain allowed symbol characters you could choose to use a symbol to represent them in EDN.
In general, people use keywords for keys of maps or for tagging, such as saying that the type of animal is :monkey:
{:animal-type :monkey}
And in general, string is used to represent free-form text. Text entered by a user, or needing to be displayed to a user.
{:animal-type :monkey
:animal-name "Bruno the monkey"}
Finally, Symbols are normally used to refer to other objects within the language itself. Such as referring to a function, another piece of data, etc.
{:animal-type :monkey
:animal-name "Bruno the monkey"
:transform-fn animal/add-owner-info}

How to change printed representation of function's derivative in sympy

In a dynamic system my base values are all functions of time, d(t). I create the variable d using d = Function('d')(t) where t = S('t')
Obviously it's very common to have derivatives of d (rates of change like velocity etc.). However the default printing of diff(d(t)) gives:-
Derivative(d(t), t)
and using pretty printing in ipython (for e.g.) gives a better looking version of:-
d/dt (d(t))
The functions which include the derivatives of d(t) are fairly long in my problems however, and I'd like the printed representation to be something like d'(t) or \dot(d)(t) (Latex).
Is this possible in sympy? I can probably workaround this using subs but would prefer a generic sympy_print function or something I could tweak.
I do this by substitution. It is horribly stupid, but it works like a charm:
q = Function('q')(t)
q_d = Function('\\dot{q}')(t)
and then substitute with
alias = {q.diff(t):q_d, } # and higher derivatives etc..
hd = q.diff(t).subs(alias)
And the output hd has a pretty dot over it's head!
As I said: this is a work-around and works, but you have to be careful in order to substitute correctly (Also for q_d.diff(t), which must be q_d2 and so on! You can have one big list with all replacements for printing and just apply it after the relevant mathematical steps.)
The vector printing module that you already found is the only place where such printing is implemented in SymPy.
from sympy.physics.vector import dynamicsymbols
from sympy.physics.vector.printing import vpprint, vlatex
d = dynamicsymbols('d')
vpprint(d.diff()) # ḋ
vlatex(d.diff()) # '\\dot{d}'
The regular printers (pretty, LaTeX, etc) do not support either prime or dot notation for derivatives. Their _print_Derivative methods are written so that they also work for multivariable expressions, where one has to specify a variable by using some sort of d/dx notation.
It would be nice to have an option for shorter derivative notation in general.

Sympy symbol and the cls parameter

I have seen f = sympy.symbols('f', cls=Function) but not any documentation. Python does not like x = sympy.symbols('x', cls=FF(8)), it complains about
raise CoercionFailed("expected an integer, got %s" % a) CoercionFailed: expected an integer, got x
Whan is the purpose of the cls parameters and what must I do so that cls=FF(8) is meaning full?
With x = sympy.symbols('x', cls=FF(8)) I want x to be a symbol in the field FF(8), i.e x^(2^8-1) must give me 1.
There are a few issues here:
The FF object does not allow Symbols. It only works for exact numerical entries, like FF(3)(2).
Therefore, the cls parameter of symbols will not work. That just changes what object is used to create the symbol, so it must take a string as an input (the default is Symbol).
SymPy does not currently support Symbols over finite fields. The best bet you can get is to use the Poly object with the modulus flag.
FF currently only supports finite fields of prime cardinality. FF(8) has actually created the ring Z_8, not the finite field with 8 elements.
You probably know this, but ^ does not do exponentiation in SymPy/Python. Use **.

Transform a lot of symbolic expression from x^2 or x^3 to pow(x,n)

I have a lot of symbolic expressions with the power symbol which I want to copy in a C++ program. However they have the power symbol ^ which is not used in C. Any advice for making the change fast? Should I use regular expressions of i can make gcc to see ^ as pow()?
To be certain not to foul anything up, you'd need a parser that knows when a ^ is actually being used as a power symbol (and not for example in a comment, a string).
But you might get away with a simple regex:
Replace (\w+)\s*\^\s*(\w+) with pow(\1,\2).
That should cover most cases and be reasonably safe.
Of course, it will fail if there are more parameters, like in (a + b) ^ (c + d) etc. This regex only matches things like a^b or 2^3. It also doesn't match x^0.5 correctly, so if those may occur, you'd need ([\w.]+)\s*\^\s*([\w.]+)

What are the allowed characters in a Clojure keyword?

I am looking for a list of the allowed characters in a clojure keyword. Specifically I am interested to know if any of the following characters are allowed: - _ /.
I am not a java programmer, so I would not know the underlying ramifications if any. I don't know if the clojure keyword is mapped to a java keyword if there is such a thing.
Edit:
When I initially composed this answer, I was probably a little too heavily invested in the question of "what can you get away with?" In fairness to myself though, the keyword admissibility issue appears to be unsettled still. So:
First, a little about keywords, for new readers:
Keywords come in two flavours, qualified and unqualified. Unqualified keywords, like :foo, have no namespace component. Qualified keywords look like :foo/bar where the part prior to the slash is the namespace, ostensibly. Keywords can't be referred, and can be given a non-existent namespace, so their namespace behaviour is different from other Clojure objects.
Keywords can be created either by literals to the reader, like :foo, or by the keyword function, which is (keyword name-str) or (keyword ns name).
Keywords evaluate to themselves only, unlike symbols which point to vars. Note that keywords are not symbols.
What is officially permitted?
According to the reader documentation a single slash is permitted, a no periods in the name, and all rules to do with symbols.
What is actually permitted?
More or less anything but spaces seem to be permitted in the reader. For instance,
user> :-_./asdfgse/aser/se
:-_./asdfgse/aser/se
Appears to be legal. The namespace for the above keyword is:
user> (namespace :-_./asdfgse/aser/se)
"-_./asdfgse/aser"
So the namespace appears to consist of everything prior to the last forward slash.
The keyword function is even more permissive:
user> (keyword "////+" "/////")
:////+//////
user> (namespace (keyword "////+" "/////"))
"////+"
And similarly, spaces are fine too if you use the keyword function. I'm not sure exactly what limitations are placed on Unicode characters, but the REPL doesn't appear to complain when I put in arbitrary characters.
What's likely to happen in the future:
There have been some rumblings about validating keywords as they are interned. Supposedly one of the longest open clojure tickets is concerned with validation of keywords. So the keyword function may cease to be so permissive in the future, though that seems to be up in the air. See the assembla ticket and google group discussion.
The "correct" answer is documented:
Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, and ? (other characters will be allowed eventually, but not all macro characters have been determined). '/' has special meaning, it can be used once in the middle of a symbol to separate the namespace from the name, e.g. my-namespace/foo. '/' by itself names the division function. '.' has special meaning - it can be used one or more times in the middle of a symbol to designate a fully-qualified class name, e.g. java.util.BitSet, or in namespace names. Symbols beginning or ending with '.' are reserved by Clojure. Symbols containing / or . are said to be 'qualified'. Symbols beginning or ending with ':' are reserved by Clojure. A symbol can contain one or more non-repeating ':'s.
Edit: And further with respect to keywords:
Keywords are like symbols, except:
* They can and must begin with a colon, e.g. :fred.
* They cannot contain '.' or name classes.
* A keyword that begins with two colons is resolved in the current namespace
From that list, the reader certainly allows - and _, but / has a special meaning as the delimiter between namespaces and symbol names. Period (which you didn't ask about) is problematic inside symbol names as well, since it is used in fully-qualified Java class names.
As far as Clojure idiom goes, - is your best friend in symbol names. It takes the place of camel case in Java or the underscore in Ruby.
starting in 1.3 you can use ' anywhere not starting a keyword. so :arthur's-keyword is allowed now :)
I use the keywords :-P and :-D to spice up my code occasionally (as synonyms for true and false)