sympy
has re
and im
functions:
In [113]: exp[complex[2,1]]
Out[113]:
1.0⋅ⅈ
7.38905609893065⋅ℯ
In [114]: re[exp[complex[2,1]]]
Out[114]: 3.99232404844127
In [115]: im[exp[complex[2,1]]]
Out[115]: 6.21767631236797
In [116]: exp[complex[2,1]].evalf[]
Out[116]: 3.99232404844127 + 6.21767631236797⋅ⅈ
.real
and .imag
are attributes [possibly implemented as properties] of numpy
arrays [and complex python numbers].
Exploring sympy
a bit more:
In [152]: expand[exp[y],complex=True]
Out[152]:
re[y] re[y]
ⅈ⋅ℯ ⋅sin[im[y]] + ℯ ⋅cos[im[y]]
In [153]: expand[exp[complex[2,1]],complex=True]
Out[153]: 3.99232404844127 + 6.21767631236797⋅ⅈ
Your sol
:
In [157]: sol
Out[157]:
⎛ ________⎞ ⎛ ________⎞
⎜ ╱ 2 ⎟ ⎜ ╱ 2 ⎟
t⋅w⋅⎝-z - ╲╱ z - 1 ⎠ t⋅w⋅⎝-z + ╲╱ z - 1 ⎠
x[t] = - C₂⋅ℯ + C₂⋅ℯ
In [181]: f1 = sol.rhs.subs[{w:10, z:0.001,C2:1}]
In [182]: f1
Out[182]:
10⋅t⋅[-0.001 - 0.999999499999875⋅ⅈ] 10⋅t⋅[-0.001 + 0.999999499999875⋅ⅈ]
- ℯ + ℯ
Making a numpy
compatible function:
In [187]: f = lambdify[t, f1]
In [188]: print[f.__doc__]
Created with lambdify. Signature:
func[t]
Expression:
-exp[10*t*[-0.001 - 0.999999499999875*I]] + exp[10*t*[-0.001 +...
Source code:
def _lambdifygenerated[t]:
return [-exp[10*t*[-0.001 - 0.999999499999875*1j]] + exp[10*t*[-0.001 + 0.999999499999875*1j]]]
Imported modules:
evaluate it at a range of values:
In [189]: f[np.arange[10]]
Out[189]:
array[[0.+0.j , 0.-1.07720771j, 0.+1.78972745j, 0.-1.91766624j,
0.+1.43181934j, 0.-0.49920326j, 0.-0.57406585j, 0.+1.44310044j,
0.-1.83494157j, 0.+1.63413971j]]
same values with just sympy
:
In [199]: [im[f1.evalf[subs={t:i}]] for i in range[10]]
Out[199]:
[0, -1.0772077135423, 1.78972744700845, -1.9176662437755, 1.43181934232583, -0.499203257243971, -0.
574065847629935, 1.44310044143674, -1.83494157235822, 1.63413971490123]
Hi, guys today we have got a very easy topic i.e exponential function in Numpy – Python.
So let’s start.
The first question comes in our mind that what is the Exponential Function and what it does?
So as we know about the exponents, this Exponential Function in Numpy is used to find the exponents of ‘e’.
We know that the value of ‘e’ is ‘2.71828183’.
If we need to find the exponential of a given array or list, the code is mentioned below.
import numpy as np #create a list l1=[1,2,3,4,5] print[np.exp[l1]]
Run this code online
The output of the following code is:-
import numpy as np l1=np.array[[1,2,3,4,5,6,7]] print[l1] print[np.exp[l1]]
Run this program online
The output of the following code is:-
Now the above things are the basics.
Here is the complete syntax of the numpy.exp
numpy.exp[array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None]
numpy.exp[] with matplotlib
As we know we can plot the graph of ‘e’. Python gives as a special module matplotlib.pyplot.
By using this module we can plot the graph of the ‘e’
Here is the example code for that.
import numpy as np import matplotlib.pyplot as plt x= np.array[[1,2,3,4,5]] y=np.exp[x] plt.plot[x,y] plt.show[]
The output of the code is a graph shown below.
Now there is a question arises that what to do if we have to plot two graphs together.
So, no need to worry because we can plot n numbers of the graphs using the module I have mentioned above.
import numpy as np import matplotlib.pyplot as plt x= np.array[[1,3,5]] y=[2,4,6] z=np.exp[x] w=np.exp[y] plt.plot[x,z,color="blue",marker='*'] plt.plot[y,w,color="red",marker='o'] plt.xlabel["X Axis--------->"] plt.ylabel["Y Axis--------->"] plt.show[]
Here is the output of the following code.
So, I hope you have understood this topic very well.