Replacing trigonometric functions in Sympy - trigonometry

I am trying to use Sympy to simplify some trigonometry. I realize that Sympy has a mature set of tools for dealing with this, so it seems like I must be missing something. However, I have spent a lot of time searching and can't seem to find exactly what I am looking for.
Question: How do you expand a simple trig function into its complex components?
MWE:
x = Symbol('x',real=True)
# The following works. Note that it expands cmplx functions into trig fncs
exp(I*x).expand(complex=True)
# Now try to go the other way
expand(sin(x)) #nope
expand_trig(sin(x)) #nope
expand(sin(x),complex=True) #nope
expand_complex(sin(x)) #nope
I also tried replace, rewrite, and xreplace, but didn't get anywhere.

Related

Trying to print a generator object with a short code

It probably has been asked before,
But I couldn't find something like that,
I'm trying to print a generator object:
n="12234451"
print(*[n[c]for c in range(len(n))if n[c]!=n[c-1]or c==0],sep="")
I usually use something like that, however, I'm wondering if it's the most efficient way to do it
(without changing the fact that the expression is in the print)
Thanks for your time

Creating graphs using code from Pari-GP, but using Sage's graphing tools

everyone!
I know this is probably a dumb question, but I'm having a lot of trouble with Sage, and it's frustrating the hell out of me. To give a bit of an explanation, I code entirely in pari-gp. I don't have a single problem with my code in pari-gp, I'm just having trouble with interfacing my code with Sage. Which, this is something Sage bolsters, that you can run pari-gp code within sage. I essentially want to run all my processes in pari-gp, but I want to exploit all of Sage's graphing protocols.
What I want to do couldn't be easier. I have a well working function test(x,y), which produces a real number. I don't have any problems running this in pari, everything is kosher. All I want to do is make a graph of test(x,y) = z. Now I know how to run this command in sage, but I don't know how to interface such that test is actually a program running from Pari code.
I'd love to program all my stuff in Sage, but that's just impossible. I need the language Pari, so before you say that I could just translate everything in Sage, it's impossible. I just can't wrap my head around how to interface Pari with Sage. I know it's possible; hell, they advertise it on the main website. I'm just very unclear about how to do the following:
1.) Read a file from my pari-gp root directory: gp.read("myfile.gp").
2.) Inherit the functions, such that gp("test(x,y)"), now runs as though it runs in the GP terminal.
3.) Graph said function test(x,y) using Sage's graphing protocols.
Any help is greatly appreciated. I know I'm just forgetting to do something dumb, or I'm missing a step in how I'm doing it.
Any help is greatly appreciated!
Let us start with:
sage: gp("test(x, y) = sin(x*y)")
(x,y)->sin(x*y)
sage: gp("test(1, 1)")
0.84147098480789650665250232163029899962
sage: type(_)
<class 'sage.interfaces.gp.GpElement'>
So the printed version of gp("test(1, 1)") looks like a number, but it is not a float instance. Note that py3 has some f-strings, so we can easily plug in values of variables x, y inside the gp("test(...)") . I will use f-strings to define the sage function...
And we are not far away from the final plot:
sage: f = lambda x, y: float( gp(f"test({x}, {y})") )
sage: plot3d(f, [0, pi], [0, pi])
Launched html viewer for Graphics3d Object

Matlab function defintion method that takes string as the definition (MFILE)

there is this method I used the other day and I have forgotten the details, which in we used a syntax like this:
f=//command//(x,'sin(x)');
something like this.
im not sure if the syntax is fully correct, or what the right command is. but after this we could simply ask for the f(x) value like this:
x= 0;
y= f(x);
and then the results were y=0;
What you are asking for is usually not recommendable. Please check if a simple anonymous function also fits your requirements:
f=#(x)(sin(x))
In case you really need to evaluate from a string:
f=str2func('#(x)sin(x)')
I would advice against the second option unless absolutely required, it can lead to hard to debug errors.
well I found the answer myself and it was "inline" command; :)
f=inline('sin(x+y+z)','x','y','z');
you can add as much variables as needed too.

pytorch - why to use Tensor.requires_grad_(True) instead of Tensor.requires_grad=True

I'm reading introduction into PyTorch and a bit confused by the Tensor.requires_grad_(...) function. Why would one would prefer it instead of just using the setter: Tensor.requires_grad = ... ? Setter syntax seems to be more clean and readable. Why to introduce this strange function with this funny underscore suffix?

How to define a function without arguments in gnuplot?

I want to define a function which returns a random number:
gnuplot> rnd() = int(rand(0)*2**24)
^
invalid command
I need add a parameter x to stop the error message:
gnuplot> rnd(x) = int(rand(0)*2**24)
Is it possible to define a function without parameters?
I don't think that is possible (look at the syntax for functions in help user-defined). As far as I can tell -- using rand is the only time when this is useful -- In all other cases your "function" would just evaluate to a constant. I'm guessing the gnuplot devs just haven't thought of this (interesting) corner case, or they didn't think it useful enough to implement.
I hope that I'm wrong about this and somebody else comes along and proves it, but I don't think I am. I think the easiest workaround it to just pass a parameter as you've already mentioned. If you really don't like that, you could use a macro:
set macro
rnd="(int(rand(0)*2**24))"
print 5+#rnd

Resources