When printing with Sympy certain symbolic expressions include brackets which may not be visually desirable. As stated in the Printing System Documentation, the sympy.latex function has an argument fold_func_brackets which will remove these brackets.
How can you get the same functionality when printing to the screen (e.g. in IPython-notebook or terminal). For example
from sympy import *
init_printing()
a, b = symbols('a, b')
cos(a) + sin(b)
The result of this is sin(b) + cos(a). However, I'd like it to print: sin b + cos a without the brackets.
Can this be done?
There's no builtin way to do this, but you could probably modify the a subclass of the printer pretty easily, and pass it in as a custom printer to init_printing(pretty_printer=Printer). You want to subclass PrettyPrinter and override _print_Function to be the same, except changing the call that uses prettyForm.FUNC.
If you want to add an option to do this to the SymPy printer, we would probably accept the pull request.
Related
I know this question has been asked and answered before, but none of the many answers work for me as described.
What is the procedure for reloading a module that I'm working on in Julia (1.6)?
For example, I have
module MyModule
export letters
const letters = String('A':'Z')
end
and I want the be able to load the module, make changes to letters in the module's file, and then reload the module and have those changes reflected in subsequent uses of letters. This seems simple enough, but I can't get it to work.
I've tried
include("src/MyModule.jl")
using .MyModule
but if I change the definition of letters in MyModule.jl and then
include("src/MyModule.jl")
letters doesn't change, unless I fully qualify its use each time with Main.MyModule.letters: using Main.MyModule; letters refers, for example, to the old definition.
How do I reload a module under development so that I can refer to its definitions without fully qualifying them (and without having an unqualified shadow definition always lying around)?
I would just use Revise.jl and wrap everything in functions:
module MyModule
export letters
letters(char_start, char_end) = char_start:char_end |> String
end
julia> using Revise
julia> includet("src/MyModule.jl")
julia> using .MyModule
julia> letters('l', 'p')
"lmnop"
module MyModule
export letters
letters(char_start, char_end) = char_start:char_start |> String
end
julia> letters('l', 'p')
"l"
const is for defining things that you do not want to modify, so I would not expect your original version to work as expected. Revise.jl should also throw a redefinition error if you try to change it
In general though, it's usually much nicer (and easier too!) to just put everything in a package and use the usual using/import syntax. PkgTemplates.jl is great for this
If you would like to redefine consts though, I would definitely recommend checking out Pluto.jl
I have this Julia script:
function f(x,y)
x+y
end
f(3, 4)
When I run this in the live terminal (via copy/paste), I get the desired result 7. But if I run the script, the output from the function is suppressed. Why is that?
Julia, unlike Matlab, doesn't automatically print values (the REPL does since that's what it's for: REPL = "read, eval, print loop"). You have to explicitly print the value using print or show, e.g. show(f(3, 4)). In this case, print and show do the same thing, but in general they have somewhat different meanings:
print([io::IO], xs...)
Write to io (or to the default output stream stdout if io is not given) a canonical (un-decorated) text representation. The representation used by print includes minimal formatting and tries to avoid Julia-specific details.
versus
show(x)
Write an informative text representation of a value to the current output stream. New types should overload show(io::IO, x) where the first argument is a stream. The representation used by show generally includes Julia-specific formatting and type information.
Note that there is also the #show macro, which prints the expression that is evaluated followed by its value, like so:
julia> #show f(3, 4);
f(3, 4) = 7
I am trying to display both sides of a vector equation.
I am able to display() the rhs of the equation but I am unable to define an equality sympy.Eq(symbols("F_T"), F_Tx*N.x + F_Ty*N.y + F_Tz*N.z) using vectors. Ideally I would like F_T to its own symbol (for use in other equations) but creating a vector modifies the type() from "symbol" to "Vector".
Therefore I am unable to print the desired full equation (like a textbook might). I am open to printing in LaTeX but I am uncertain how to combine my sympy expressions with created LaTeX. Any help is appreciated, Thanks!
I would like to be able to pretty-print: F_T = F_Tx*N.x + F_Ty*N.y + F_Tz*N.z
I've been wondering about how to do the same thing. The following tips are not a perfect solution, but they may prove useful to someone.
def create_vector(sym, refFrame):
unit_vectors = refFrame.base_vectors()
syms = sp.symbols(' '.join([sym + '_{' + str(ax) + '}' for ax in unit_vectors]))
vector = reduce(add, [sym * unit for sym, unit in zip(syms, unit_vectors)])
return (vector,) + syms
This function allows you to quickly create a vector with unresolved symbolic components, like so:
XYZ = CoordSys3D('XYZ', vector_names=('I', 'J', 'K'))
r_rel, r_rel_i, r_rel_j, r_Rel_k = create_vector('\mathbf{r_{rel}}', XYZ)
r_rel
Now, if you try to render this in Jupyter, you get the ugly component representation:
But, if you are prepared to be a bit hacky, you can work around this. The Zero vector stores it's own latex representation internally, as self._latex_form. The sympy latex
printer determines if a vector is the zero vector by checking if expr == expr.zero.
We can abuse these and trick the latex printer into printing a nicer representation:
r_rel.zero = r_rel
r_rel._latex_form = '\mathbf{r_{rel}}'
r_rel
For which we get this prettier representation:
Admittedly, this could break things, and it will make you feel dirty. But it may help someone.
Try creating an equality to display: eq = Eq(F_T, F_Tx*N.x ...). An equality is printed with an equals sign separating the lhs and rhs of the equality.
Problem
I read in an array of strings from a file.
julia> file = open("word-pairs.txt");
julia> lines = readlines(file);
But Julia doesn't know that they're strings.
julia> typeof(lines)
Array{Any,1}
Question
Can I tell Julia this somehow?
Is it possible to insert type information onto a computed result?
It would be helpful to know the context where this is an issue, because there might be a better way to express what you need - or there could be a subtle bug somewhere.
Can I tell Julia this somehow?
No, because the readlines function explicitly creates an Any array (a = {}): https://github.com/JuliaLang/julia/blob/master/base/io.jl#L230
Is it possible to insert type information onto a computed result?
You can convert the array:
r = convert(Array{ASCIIString,1}, w)
Or, create your own readstrings function based on the link above, but using ASCIIString[] for the collection array instead of {}.
Isaiah is right about the limits of readlines. More generally, often you can say
n = length(A)::Int
when generic type inference fails but you can guarantee the type in your particular case.
As of 0.3.4:
julia> typeof(lines)
Array{Union(ASCIIString,UTF8String),1}
I just wanted to warn against:
convert(Array{ASCIIString,1}, lines)
that can fail (for non-ASCII) while I guess, in this case nothing needs to be done, this should work:
convert(Array{UTF8String,1}, lines)
Most of the D language tutorials I've seen use printf to output text to the console, but that can't be right. I know that D provides direct access to the C/C++ libraries, but shouldn't D's console output function be used instead? What is the preferred method for outputting text (formatted or otherwise) to a console window?
Within the module std.stdio, you'll find write and friends: writeln, writef, and writefln.
write just takes each argument, converts it to a string, and outputs it:
import std.stdio;
void main()
{
write(5, " <- that's five"); // prints: 5 <- that's five
}
writef treats the first string as a format-specifier (much like C's printf), and uses it to format the remaining arguments:
import std.stdio;
void main()
{
writef("%d %s", 5, "<- that's five"); // prints: 5 <- that's five
}
The versions ending with "ln" are equivalent to the version without it, but also append a newline at the end of printing. All versions are type-safe (and therefore extensible).
The use of printf is mostly historical. It has been used because it is declared in one of the modules that is automatically imported and that make the examples shorter. Also, the author of D wrote many of the examples and IIRC, while debugging the compiler he prefers printf over writef because there is less to go wrong. That plus muscle memory results in printf leaking into examples.