Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- llmeval-env/lib/python3.10/site-packages/nvidia/cudnn/lib/libcudnn_adv_infer.so.8 +3 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/hydrogen.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/matrices.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/paulialgebra.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/pring.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/qho_1d.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/secondquant.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/sho.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/wigner.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__init__.py +15 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__pycache__/control_plots.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__pycache__/lti.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/control_plots.py +961 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/lti.py +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__pycache__/test_control_plots.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__pycache__/test_lti.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/test_control_plots.py +300 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/test_lti.py +1245 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/__pycache__/test_gamma_matrices.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/test_gamma_matrices.py +427 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__init__.py +38 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/gaussopt.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/medium.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/polarization.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/utils.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/waves.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/gaussopt.py +923 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/medium.py +253 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/polarization.py +732 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_gaussopt.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_medium.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_polarization.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_utils.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_waves.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_gaussopt.py +102 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_medium.py +48 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_polarization.py +57 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_utils.py +202 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_waves.py +82 -0
- llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/utils.py +698 -0
.gitattributes
CHANGED
@@ -186,3 +186,4 @@ env-llmeval/lib/python3.10/site-packages/pyarrow/libarrow_substrait.so.1500 filt
|
|
186 |
env-llmeval/lib/python3.10/site-packages/pyarrow/lib.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
187 |
env-llmeval/lib/python3.10/site-packages/pyarrow/libarrow_flight.so.1500 filter=lfs diff=lfs merge=lfs -text
|
188 |
llmeval-env/lib/python3.10/site-packages/scipy/stats/_unuran/unuran_wrapper.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
186 |
env-llmeval/lib/python3.10/site-packages/pyarrow/lib.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
187 |
env-llmeval/lib/python3.10/site-packages/pyarrow/libarrow_flight.so.1500 filter=lfs diff=lfs merge=lfs -text
|
188 |
llmeval-env/lib/python3.10/site-packages/scipy/stats/_unuran/unuran_wrapper.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
189 |
+
llmeval-env/lib/python3.10/site-packages/nvidia/cudnn/lib/libcudnn_adv_infer.so.8 filter=lfs diff=lfs merge=lfs -text
|
llmeval-env/lib/python3.10/site-packages/nvidia/cudnn/lib/libcudnn_adv_infer.so.8
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:50c02f961775d3aca2676369156435d964b5615719f8b44b49cd73edc21217f3
|
3 |
+
size 125141304
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (427 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/hydrogen.cpython-310.pyc
ADDED
Binary file (7.37 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/matrices.cpython-310.pyc
ADDED
Binary file (3.64 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/paulialgebra.cpython-310.pyc
ADDED
Binary file (6.15 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/pring.cpython-310.pyc
ADDED
Binary file (2.6 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/qho_1d.cpython-310.pyc
ADDED
Binary file (2.43 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/secondquant.cpython-310.pyc
ADDED
Binary file (83.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/sho.cpython-310.pyc
ADDED
Binary file (2.82 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/__pycache__/wigner.cpython-310.pyc
ADDED
Binary file (37.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__init__.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .lti import (TransferFunction, Series, MIMOSeries, Parallel, MIMOParallel,
|
2 |
+
Feedback, MIMOFeedback, TransferFunctionMatrix, bilinear, backward_diff)
|
3 |
+
from .control_plots import (pole_zero_numerical_data, pole_zero_plot, step_response_numerical_data,
|
4 |
+
step_response_plot, impulse_response_numerical_data, impulse_response_plot, ramp_response_numerical_data,
|
5 |
+
ramp_response_plot, bode_magnitude_numerical_data, bode_phase_numerical_data, bode_magnitude_plot,
|
6 |
+
bode_phase_plot, bode_plot)
|
7 |
+
|
8 |
+
__all__ = ['TransferFunction', 'Series', 'MIMOSeries', 'Parallel',
|
9 |
+
'MIMOParallel', 'Feedback', 'MIMOFeedback', 'TransferFunctionMatrix','bilinear',
|
10 |
+
'backward_diff', 'pole_zero_numerical_data',
|
11 |
+
'pole_zero_plot', 'step_response_numerical_data', 'step_response_plot',
|
12 |
+
'impulse_response_numerical_data', 'impulse_response_plot',
|
13 |
+
'ramp_response_numerical_data', 'ramp_response_plot',
|
14 |
+
'bode_magnitude_numerical_data', 'bode_phase_numerical_data',
|
15 |
+
'bode_magnitude_plot', 'bode_phase_plot', 'bode_plot']
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.02 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__pycache__/control_plots.cpython-310.pyc
ADDED
Binary file (30 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/__pycache__/lti.cpython-310.pyc
ADDED
Binary file (118 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/control_plots.py
ADDED
@@ -0,0 +1,961 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import I, pi
|
2 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
3 |
+
from sympy.polys.partfrac import apart
|
4 |
+
from sympy.core.symbol import Dummy
|
5 |
+
from sympy.external import import_module
|
6 |
+
from sympy.functions import arg, Abs
|
7 |
+
from sympy.integrals.laplace import _fast_inverse_laplace
|
8 |
+
from sympy.physics.control.lti import SISOLinearTimeInvariant
|
9 |
+
from sympy.plotting.plot import LineOver1DRangeSeries
|
10 |
+
from sympy.polys.polytools import Poly
|
11 |
+
from sympy.printing.latex import latex
|
12 |
+
|
13 |
+
__all__ = ['pole_zero_numerical_data', 'pole_zero_plot',
|
14 |
+
'step_response_numerical_data', 'step_response_plot',
|
15 |
+
'impulse_response_numerical_data', 'impulse_response_plot',
|
16 |
+
'ramp_response_numerical_data', 'ramp_response_plot',
|
17 |
+
'bode_magnitude_numerical_data', 'bode_phase_numerical_data',
|
18 |
+
'bode_magnitude_plot', 'bode_phase_plot', 'bode_plot']
|
19 |
+
|
20 |
+
matplotlib = import_module(
|
21 |
+
'matplotlib', import_kwargs={'fromlist': ['pyplot']},
|
22 |
+
catch=(RuntimeError,))
|
23 |
+
|
24 |
+
numpy = import_module('numpy')
|
25 |
+
|
26 |
+
if matplotlib:
|
27 |
+
plt = matplotlib.pyplot
|
28 |
+
|
29 |
+
if numpy:
|
30 |
+
np = numpy # Matplotlib already has numpy as a compulsory dependency. No need to install it separately.
|
31 |
+
|
32 |
+
|
33 |
+
def _check_system(system):
|
34 |
+
"""Function to check whether the dynamical system passed for plots is
|
35 |
+
compatible or not."""
|
36 |
+
if not isinstance(system, SISOLinearTimeInvariant):
|
37 |
+
raise NotImplementedError("Only SISO LTI systems are currently supported.")
|
38 |
+
sys = system.to_expr()
|
39 |
+
len_free_symbols = len(sys.free_symbols)
|
40 |
+
if len_free_symbols > 1:
|
41 |
+
raise ValueError("Extra degree of freedom found. Make sure"
|
42 |
+
" that there are no free symbols in the dynamical system other"
|
43 |
+
" than the variable of Laplace transform.")
|
44 |
+
if sys.has(exp):
|
45 |
+
# Should test that exp is not part of a constant, in which case
|
46 |
+
# no exception is required, compare exp(s) with s*exp(1)
|
47 |
+
raise NotImplementedError("Time delay terms are not supported.")
|
48 |
+
|
49 |
+
|
50 |
+
def pole_zero_numerical_data(system):
|
51 |
+
"""
|
52 |
+
Returns the numerical data of poles and zeros of the system.
|
53 |
+
It is internally used by ``pole_zero_plot`` to get the data
|
54 |
+
for plotting poles and zeros. Users can use this data to further
|
55 |
+
analyse the dynamics of the system or plot using a different
|
56 |
+
backend/plotting-module.
|
57 |
+
|
58 |
+
Parameters
|
59 |
+
==========
|
60 |
+
|
61 |
+
system : SISOLinearTimeInvariant
|
62 |
+
The system for which the pole-zero data is to be computed.
|
63 |
+
|
64 |
+
Returns
|
65 |
+
=======
|
66 |
+
|
67 |
+
tuple : (zeros, poles)
|
68 |
+
zeros = Zeros of the system. NumPy array of complex numbers.
|
69 |
+
poles = Poles of the system. NumPy array of complex numbers.
|
70 |
+
|
71 |
+
Raises
|
72 |
+
======
|
73 |
+
|
74 |
+
NotImplementedError
|
75 |
+
When a SISO LTI system is not passed.
|
76 |
+
|
77 |
+
When time delay terms are present in the system.
|
78 |
+
|
79 |
+
ValueError
|
80 |
+
When more than one free symbol is present in the system.
|
81 |
+
The only variable in the transfer function should be
|
82 |
+
the variable of the Laplace transform.
|
83 |
+
|
84 |
+
Examples
|
85 |
+
========
|
86 |
+
|
87 |
+
>>> from sympy.abc import s
|
88 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
89 |
+
>>> from sympy.physics.control.control_plots import pole_zero_numerical_data
|
90 |
+
>>> tf1 = TransferFunction(s**2 + 1, s**4 + 4*s**3 + 6*s**2 + 5*s + 2, s)
|
91 |
+
>>> pole_zero_numerical_data(tf1) # doctest: +SKIP
|
92 |
+
([-0.+1.j 0.-1.j], [-2. +0.j -0.5+0.8660254j -0.5-0.8660254j -1. +0.j ])
|
93 |
+
|
94 |
+
See Also
|
95 |
+
========
|
96 |
+
|
97 |
+
pole_zero_plot
|
98 |
+
|
99 |
+
"""
|
100 |
+
_check_system(system)
|
101 |
+
system = system.doit() # Get the equivalent TransferFunction object.
|
102 |
+
|
103 |
+
num_poly = Poly(system.num, system.var).all_coeffs()
|
104 |
+
den_poly = Poly(system.den, system.var).all_coeffs()
|
105 |
+
|
106 |
+
num_poly = np.array(num_poly, dtype=np.complex128)
|
107 |
+
den_poly = np.array(den_poly, dtype=np.complex128)
|
108 |
+
|
109 |
+
zeros = np.roots(num_poly)
|
110 |
+
poles = np.roots(den_poly)
|
111 |
+
|
112 |
+
return zeros, poles
|
113 |
+
|
114 |
+
|
115 |
+
def pole_zero_plot(system, pole_color='blue', pole_markersize=10,
|
116 |
+
zero_color='orange', zero_markersize=7, grid=True, show_axes=True,
|
117 |
+
show=True, **kwargs):
|
118 |
+
r"""
|
119 |
+
Returns the Pole-Zero plot (also known as PZ Plot or PZ Map) of a system.
|
120 |
+
|
121 |
+
A Pole-Zero plot is a graphical representation of a system's poles and
|
122 |
+
zeros. It is plotted on a complex plane, with circular markers representing
|
123 |
+
the system's zeros and 'x' shaped markers representing the system's poles.
|
124 |
+
|
125 |
+
Parameters
|
126 |
+
==========
|
127 |
+
|
128 |
+
system : SISOLinearTimeInvariant type systems
|
129 |
+
The system for which the pole-zero plot is to be computed.
|
130 |
+
pole_color : str, tuple, optional
|
131 |
+
The color of the pole points on the plot. Default color
|
132 |
+
is blue. The color can be provided as a matplotlib color string,
|
133 |
+
or a 3-tuple of floats each in the 0-1 range.
|
134 |
+
pole_markersize : Number, optional
|
135 |
+
The size of the markers used to mark the poles in the plot.
|
136 |
+
Default pole markersize is 10.
|
137 |
+
zero_color : str, tuple, optional
|
138 |
+
The color of the zero points on the plot. Default color
|
139 |
+
is orange. The color can be provided as a matplotlib color string,
|
140 |
+
or a 3-tuple of floats each in the 0-1 range.
|
141 |
+
zero_markersize : Number, optional
|
142 |
+
The size of the markers used to mark the zeros in the plot.
|
143 |
+
Default zero markersize is 7.
|
144 |
+
grid : boolean, optional
|
145 |
+
If ``True``, the plot will have a grid. Defaults to True.
|
146 |
+
show_axes : boolean, optional
|
147 |
+
If ``True``, the coordinate axes will be shown. Defaults to False.
|
148 |
+
show : boolean, optional
|
149 |
+
If ``True``, the plot will be displayed otherwise
|
150 |
+
the equivalent matplotlib ``plot`` object will be returned.
|
151 |
+
Defaults to True.
|
152 |
+
|
153 |
+
Examples
|
154 |
+
========
|
155 |
+
|
156 |
+
.. plot::
|
157 |
+
:context: close-figs
|
158 |
+
:format: doctest
|
159 |
+
:include-source: True
|
160 |
+
|
161 |
+
>>> from sympy.abc import s
|
162 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
163 |
+
>>> from sympy.physics.control.control_plots import pole_zero_plot
|
164 |
+
>>> tf1 = TransferFunction(s**2 + 1, s**4 + 4*s**3 + 6*s**2 + 5*s + 2, s)
|
165 |
+
>>> pole_zero_plot(tf1) # doctest: +SKIP
|
166 |
+
|
167 |
+
See Also
|
168 |
+
========
|
169 |
+
|
170 |
+
pole_zero_numerical_data
|
171 |
+
|
172 |
+
References
|
173 |
+
==========
|
174 |
+
|
175 |
+
.. [1] https://en.wikipedia.org/wiki/Pole%E2%80%93zero_plot
|
176 |
+
|
177 |
+
"""
|
178 |
+
zeros, poles = pole_zero_numerical_data(system)
|
179 |
+
|
180 |
+
zero_real = np.real(zeros)
|
181 |
+
zero_imag = np.imag(zeros)
|
182 |
+
|
183 |
+
pole_real = np.real(poles)
|
184 |
+
pole_imag = np.imag(poles)
|
185 |
+
|
186 |
+
plt.plot(pole_real, pole_imag, 'x', mfc='none',
|
187 |
+
markersize=pole_markersize, color=pole_color)
|
188 |
+
plt.plot(zero_real, zero_imag, 'o', markersize=zero_markersize,
|
189 |
+
color=zero_color)
|
190 |
+
plt.xlabel('Real Axis')
|
191 |
+
plt.ylabel('Imaginary Axis')
|
192 |
+
plt.title(f'Poles and Zeros of ${latex(system)}$', pad=20)
|
193 |
+
|
194 |
+
if grid:
|
195 |
+
plt.grid()
|
196 |
+
if show_axes:
|
197 |
+
plt.axhline(0, color='black')
|
198 |
+
plt.axvline(0, color='black')
|
199 |
+
if show:
|
200 |
+
plt.show()
|
201 |
+
return
|
202 |
+
|
203 |
+
return plt
|
204 |
+
|
205 |
+
|
206 |
+
def step_response_numerical_data(system, prec=8, lower_limit=0,
|
207 |
+
upper_limit=10, **kwargs):
|
208 |
+
"""
|
209 |
+
Returns the numerical values of the points in the step response plot
|
210 |
+
of a SISO continuous-time system. By default, adaptive sampling
|
211 |
+
is used. If the user wants to instead get an uniformly
|
212 |
+
sampled response, then ``adaptive`` kwarg should be passed ``False``
|
213 |
+
and ``nb_of_points`` must be passed as additional kwargs.
|
214 |
+
Refer to the parameters of class :class:`sympy.plotting.plot.LineOver1DRangeSeries`
|
215 |
+
for more details.
|
216 |
+
|
217 |
+
Parameters
|
218 |
+
==========
|
219 |
+
|
220 |
+
system : SISOLinearTimeInvariant
|
221 |
+
The system for which the unit step response data is to be computed.
|
222 |
+
prec : int, optional
|
223 |
+
The decimal point precision for the point coordinate values.
|
224 |
+
Defaults to 8.
|
225 |
+
lower_limit : Number, optional
|
226 |
+
The lower limit of the plot range. Defaults to 0.
|
227 |
+
upper_limit : Number, optional
|
228 |
+
The upper limit of the plot range. Defaults to 10.
|
229 |
+
kwargs :
|
230 |
+
Additional keyword arguments are passed to the underlying
|
231 |
+
:class:`sympy.plotting.plot.LineOver1DRangeSeries` class.
|
232 |
+
|
233 |
+
Returns
|
234 |
+
=======
|
235 |
+
|
236 |
+
tuple : (x, y)
|
237 |
+
x = Time-axis values of the points in the step response. NumPy array.
|
238 |
+
y = Amplitude-axis values of the points in the step response. NumPy array.
|
239 |
+
|
240 |
+
Raises
|
241 |
+
======
|
242 |
+
|
243 |
+
NotImplementedError
|
244 |
+
When a SISO LTI system is not passed.
|
245 |
+
|
246 |
+
When time delay terms are present in the system.
|
247 |
+
|
248 |
+
ValueError
|
249 |
+
When more than one free symbol is present in the system.
|
250 |
+
The only variable in the transfer function should be
|
251 |
+
the variable of the Laplace transform.
|
252 |
+
|
253 |
+
When ``lower_limit`` parameter is less than 0.
|
254 |
+
|
255 |
+
Examples
|
256 |
+
========
|
257 |
+
|
258 |
+
>>> from sympy.abc import s
|
259 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
260 |
+
>>> from sympy.physics.control.control_plots import step_response_numerical_data
|
261 |
+
>>> tf1 = TransferFunction(s, s**2 + 5*s + 8, s)
|
262 |
+
>>> step_response_numerical_data(tf1) # doctest: +SKIP
|
263 |
+
([0.0, 0.025413462339411542, 0.0484508722725343, ... , 9.670250533855183, 9.844291913708725, 10.0],
|
264 |
+
[0.0, 0.023844582399907256, 0.042894276802320226, ..., 6.828770759094287e-12, 6.456457160755703e-12])
|
265 |
+
|
266 |
+
See Also
|
267 |
+
========
|
268 |
+
|
269 |
+
step_response_plot
|
270 |
+
|
271 |
+
"""
|
272 |
+
if lower_limit < 0:
|
273 |
+
raise ValueError("Lower limit of time must be greater "
|
274 |
+
"than or equal to zero.")
|
275 |
+
_check_system(system)
|
276 |
+
_x = Dummy("x")
|
277 |
+
expr = system.to_expr()/(system.var)
|
278 |
+
expr = apart(expr, system.var, full=True)
|
279 |
+
_y = _fast_inverse_laplace(expr, system.var, _x).evalf(prec)
|
280 |
+
return LineOver1DRangeSeries(_y, (_x, lower_limit, upper_limit),
|
281 |
+
**kwargs).get_points()
|
282 |
+
|
283 |
+
|
284 |
+
def step_response_plot(system, color='b', prec=8, lower_limit=0,
|
285 |
+
upper_limit=10, show_axes=False, grid=True, show=True, **kwargs):
|
286 |
+
r"""
|
287 |
+
Returns the unit step response of a continuous-time system. It is
|
288 |
+
the response of the system when the input signal is a step function.
|
289 |
+
|
290 |
+
Parameters
|
291 |
+
==========
|
292 |
+
|
293 |
+
system : SISOLinearTimeInvariant type
|
294 |
+
The LTI SISO system for which the Step Response is to be computed.
|
295 |
+
color : str, tuple, optional
|
296 |
+
The color of the line. Default is Blue.
|
297 |
+
show : boolean, optional
|
298 |
+
If ``True``, the plot will be displayed otherwise
|
299 |
+
the equivalent matplotlib ``plot`` object will be returned.
|
300 |
+
Defaults to True.
|
301 |
+
lower_limit : Number, optional
|
302 |
+
The lower limit of the plot range. Defaults to 0.
|
303 |
+
upper_limit : Number, optional
|
304 |
+
The upper limit of the plot range. Defaults to 10.
|
305 |
+
prec : int, optional
|
306 |
+
The decimal point precision for the point coordinate values.
|
307 |
+
Defaults to 8.
|
308 |
+
show_axes : boolean, optional
|
309 |
+
If ``True``, the coordinate axes will be shown. Defaults to False.
|
310 |
+
grid : boolean, optional
|
311 |
+
If ``True``, the plot will have a grid. Defaults to True.
|
312 |
+
|
313 |
+
Examples
|
314 |
+
========
|
315 |
+
|
316 |
+
.. plot::
|
317 |
+
:context: close-figs
|
318 |
+
:format: doctest
|
319 |
+
:include-source: True
|
320 |
+
|
321 |
+
>>> from sympy.abc import s
|
322 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
323 |
+
>>> from sympy.physics.control.control_plots import step_response_plot
|
324 |
+
>>> tf1 = TransferFunction(8*s**2 + 18*s + 32, s**3 + 6*s**2 + 14*s + 24, s)
|
325 |
+
>>> step_response_plot(tf1) # doctest: +SKIP
|
326 |
+
|
327 |
+
See Also
|
328 |
+
========
|
329 |
+
|
330 |
+
impulse_response_plot, ramp_response_plot
|
331 |
+
|
332 |
+
References
|
333 |
+
==========
|
334 |
+
|
335 |
+
.. [1] https://www.mathworks.com/help/control/ref/lti.step.html
|
336 |
+
|
337 |
+
"""
|
338 |
+
x, y = step_response_numerical_data(system, prec=prec,
|
339 |
+
lower_limit=lower_limit, upper_limit=upper_limit, **kwargs)
|
340 |
+
plt.plot(x, y, color=color)
|
341 |
+
plt.xlabel('Time (s)')
|
342 |
+
plt.ylabel('Amplitude')
|
343 |
+
plt.title(f'Unit Step Response of ${latex(system)}$', pad=20)
|
344 |
+
|
345 |
+
if grid:
|
346 |
+
plt.grid()
|
347 |
+
if show_axes:
|
348 |
+
plt.axhline(0, color='black')
|
349 |
+
plt.axvline(0, color='black')
|
350 |
+
if show:
|
351 |
+
plt.show()
|
352 |
+
return
|
353 |
+
|
354 |
+
return plt
|
355 |
+
|
356 |
+
|
357 |
+
def impulse_response_numerical_data(system, prec=8, lower_limit=0,
|
358 |
+
upper_limit=10, **kwargs):
|
359 |
+
"""
|
360 |
+
Returns the numerical values of the points in the impulse response plot
|
361 |
+
of a SISO continuous-time system. By default, adaptive sampling
|
362 |
+
is used. If the user wants to instead get an uniformly
|
363 |
+
sampled response, then ``adaptive`` kwarg should be passed ``False``
|
364 |
+
and ``nb_of_points`` must be passed as additional kwargs.
|
365 |
+
Refer to the parameters of class :class:`sympy.plotting.plot.LineOver1DRangeSeries`
|
366 |
+
for more details.
|
367 |
+
|
368 |
+
Parameters
|
369 |
+
==========
|
370 |
+
|
371 |
+
system : SISOLinearTimeInvariant
|
372 |
+
The system for which the impulse response data is to be computed.
|
373 |
+
prec : int, optional
|
374 |
+
The decimal point precision for the point coordinate values.
|
375 |
+
Defaults to 8.
|
376 |
+
lower_limit : Number, optional
|
377 |
+
The lower limit of the plot range. Defaults to 0.
|
378 |
+
upper_limit : Number, optional
|
379 |
+
The upper limit of the plot range. Defaults to 10.
|
380 |
+
kwargs :
|
381 |
+
Additional keyword arguments are passed to the underlying
|
382 |
+
:class:`sympy.plotting.plot.LineOver1DRangeSeries` class.
|
383 |
+
|
384 |
+
Returns
|
385 |
+
=======
|
386 |
+
|
387 |
+
tuple : (x, y)
|
388 |
+
x = Time-axis values of the points in the impulse response. NumPy array.
|
389 |
+
y = Amplitude-axis values of the points in the impulse response. NumPy array.
|
390 |
+
|
391 |
+
Raises
|
392 |
+
======
|
393 |
+
|
394 |
+
NotImplementedError
|
395 |
+
When a SISO LTI system is not passed.
|
396 |
+
|
397 |
+
When time delay terms are present in the system.
|
398 |
+
|
399 |
+
ValueError
|
400 |
+
When more than one free symbol is present in the system.
|
401 |
+
The only variable in the transfer function should be
|
402 |
+
the variable of the Laplace transform.
|
403 |
+
|
404 |
+
When ``lower_limit`` parameter is less than 0.
|
405 |
+
|
406 |
+
Examples
|
407 |
+
========
|
408 |
+
|
409 |
+
>>> from sympy.abc import s
|
410 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
411 |
+
>>> from sympy.physics.control.control_plots import impulse_response_numerical_data
|
412 |
+
>>> tf1 = TransferFunction(s, s**2 + 5*s + 8, s)
|
413 |
+
>>> impulse_response_numerical_data(tf1) # doctest: +SKIP
|
414 |
+
([0.0, 0.06616480200395854,... , 9.854500743565858, 10.0],
|
415 |
+
[0.9999999799999999, 0.7042848373025861,...,7.170748906965121e-13, -5.1901263495547205e-12])
|
416 |
+
|
417 |
+
See Also
|
418 |
+
========
|
419 |
+
|
420 |
+
impulse_response_plot
|
421 |
+
|
422 |
+
"""
|
423 |
+
if lower_limit < 0:
|
424 |
+
raise ValueError("Lower limit of time must be greater "
|
425 |
+
"than or equal to zero.")
|
426 |
+
_check_system(system)
|
427 |
+
_x = Dummy("x")
|
428 |
+
expr = system.to_expr()
|
429 |
+
expr = apart(expr, system.var, full=True)
|
430 |
+
_y = _fast_inverse_laplace(expr, system.var, _x).evalf(prec)
|
431 |
+
return LineOver1DRangeSeries(_y, (_x, lower_limit, upper_limit),
|
432 |
+
**kwargs).get_points()
|
433 |
+
|
434 |
+
|
435 |
+
def impulse_response_plot(system, color='b', prec=8, lower_limit=0,
|
436 |
+
upper_limit=10, show_axes=False, grid=True, show=True, **kwargs):
|
437 |
+
r"""
|
438 |
+
Returns the unit impulse response (Input is the Dirac-Delta Function) of a
|
439 |
+
continuous-time system.
|
440 |
+
|
441 |
+
Parameters
|
442 |
+
==========
|
443 |
+
|
444 |
+
system : SISOLinearTimeInvariant type
|
445 |
+
The LTI SISO system for which the Impulse Response is to be computed.
|
446 |
+
color : str, tuple, optional
|
447 |
+
The color of the line. Default is Blue.
|
448 |
+
show : boolean, optional
|
449 |
+
If ``True``, the plot will be displayed otherwise
|
450 |
+
the equivalent matplotlib ``plot`` object will be returned.
|
451 |
+
Defaults to True.
|
452 |
+
lower_limit : Number, optional
|
453 |
+
The lower limit of the plot range. Defaults to 0.
|
454 |
+
upper_limit : Number, optional
|
455 |
+
The upper limit of the plot range. Defaults to 10.
|
456 |
+
prec : int, optional
|
457 |
+
The decimal point precision for the point coordinate values.
|
458 |
+
Defaults to 8.
|
459 |
+
show_axes : boolean, optional
|
460 |
+
If ``True``, the coordinate axes will be shown. Defaults to False.
|
461 |
+
grid : boolean, optional
|
462 |
+
If ``True``, the plot will have a grid. Defaults to True.
|
463 |
+
|
464 |
+
Examples
|
465 |
+
========
|
466 |
+
|
467 |
+
.. plot::
|
468 |
+
:context: close-figs
|
469 |
+
:format: doctest
|
470 |
+
:include-source: True
|
471 |
+
|
472 |
+
>>> from sympy.abc import s
|
473 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
474 |
+
>>> from sympy.physics.control.control_plots import impulse_response_plot
|
475 |
+
>>> tf1 = TransferFunction(8*s**2 + 18*s + 32, s**3 + 6*s**2 + 14*s + 24, s)
|
476 |
+
>>> impulse_response_plot(tf1) # doctest: +SKIP
|
477 |
+
|
478 |
+
See Also
|
479 |
+
========
|
480 |
+
|
481 |
+
step_response_plot, ramp_response_plot
|
482 |
+
|
483 |
+
References
|
484 |
+
==========
|
485 |
+
|
486 |
+
.. [1] https://www.mathworks.com/help/control/ref/lti.impulse.html
|
487 |
+
|
488 |
+
"""
|
489 |
+
x, y = impulse_response_numerical_data(system, prec=prec,
|
490 |
+
lower_limit=lower_limit, upper_limit=upper_limit, **kwargs)
|
491 |
+
plt.plot(x, y, color=color)
|
492 |
+
plt.xlabel('Time (s)')
|
493 |
+
plt.ylabel('Amplitude')
|
494 |
+
plt.title(f'Impulse Response of ${latex(system)}$', pad=20)
|
495 |
+
|
496 |
+
if grid:
|
497 |
+
plt.grid()
|
498 |
+
if show_axes:
|
499 |
+
plt.axhline(0, color='black')
|
500 |
+
plt.axvline(0, color='black')
|
501 |
+
if show:
|
502 |
+
plt.show()
|
503 |
+
return
|
504 |
+
|
505 |
+
return plt
|
506 |
+
|
507 |
+
|
508 |
+
def ramp_response_numerical_data(system, slope=1, prec=8,
|
509 |
+
lower_limit=0, upper_limit=10, **kwargs):
|
510 |
+
"""
|
511 |
+
Returns the numerical values of the points in the ramp response plot
|
512 |
+
of a SISO continuous-time system. By default, adaptive sampling
|
513 |
+
is used. If the user wants to instead get an uniformly
|
514 |
+
sampled response, then ``adaptive`` kwarg should be passed ``False``
|
515 |
+
and ``nb_of_points`` must be passed as additional kwargs.
|
516 |
+
Refer to the parameters of class :class:`sympy.plotting.plot.LineOver1DRangeSeries`
|
517 |
+
for more details.
|
518 |
+
|
519 |
+
Parameters
|
520 |
+
==========
|
521 |
+
|
522 |
+
system : SISOLinearTimeInvariant
|
523 |
+
The system for which the ramp response data is to be computed.
|
524 |
+
slope : Number, optional
|
525 |
+
The slope of the input ramp function. Defaults to 1.
|
526 |
+
prec : int, optional
|
527 |
+
The decimal point precision for the point coordinate values.
|
528 |
+
Defaults to 8.
|
529 |
+
lower_limit : Number, optional
|
530 |
+
The lower limit of the plot range. Defaults to 0.
|
531 |
+
upper_limit : Number, optional
|
532 |
+
The upper limit of the plot range. Defaults to 10.
|
533 |
+
kwargs :
|
534 |
+
Additional keyword arguments are passed to the underlying
|
535 |
+
:class:`sympy.plotting.plot.LineOver1DRangeSeries` class.
|
536 |
+
|
537 |
+
Returns
|
538 |
+
=======
|
539 |
+
|
540 |
+
tuple : (x, y)
|
541 |
+
x = Time-axis values of the points in the ramp response plot. NumPy array.
|
542 |
+
y = Amplitude-axis values of the points in the ramp response plot. NumPy array.
|
543 |
+
|
544 |
+
Raises
|
545 |
+
======
|
546 |
+
|
547 |
+
NotImplementedError
|
548 |
+
When a SISO LTI system is not passed.
|
549 |
+
|
550 |
+
When time delay terms are present in the system.
|
551 |
+
|
552 |
+
ValueError
|
553 |
+
When more than one free symbol is present in the system.
|
554 |
+
The only variable in the transfer function should be
|
555 |
+
the variable of the Laplace transform.
|
556 |
+
|
557 |
+
When ``lower_limit`` parameter is less than 0.
|
558 |
+
|
559 |
+
When ``slope`` is negative.
|
560 |
+
|
561 |
+
Examples
|
562 |
+
========
|
563 |
+
|
564 |
+
>>> from sympy.abc import s
|
565 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
566 |
+
>>> from sympy.physics.control.control_plots import ramp_response_numerical_data
|
567 |
+
>>> tf1 = TransferFunction(s, s**2 + 5*s + 8, s)
|
568 |
+
>>> ramp_response_numerical_data(tf1) # doctest: +SKIP
|
569 |
+
(([0.0, 0.12166980856813935,..., 9.861246379582118, 10.0],
|
570 |
+
[1.4504508011325967e-09, 0.006046440489058766,..., 0.12499999999568202, 0.12499999999661349]))
|
571 |
+
|
572 |
+
See Also
|
573 |
+
========
|
574 |
+
|
575 |
+
ramp_response_plot
|
576 |
+
|
577 |
+
"""
|
578 |
+
if slope < 0:
|
579 |
+
raise ValueError("Slope must be greater than or equal"
|
580 |
+
" to zero.")
|
581 |
+
if lower_limit < 0:
|
582 |
+
raise ValueError("Lower limit of time must be greater "
|
583 |
+
"than or equal to zero.")
|
584 |
+
_check_system(system)
|
585 |
+
_x = Dummy("x")
|
586 |
+
expr = (slope*system.to_expr())/((system.var)**2)
|
587 |
+
expr = apart(expr, system.var, full=True)
|
588 |
+
_y = _fast_inverse_laplace(expr, system.var, _x).evalf(prec)
|
589 |
+
return LineOver1DRangeSeries(_y, (_x, lower_limit, upper_limit),
|
590 |
+
**kwargs).get_points()
|
591 |
+
|
592 |
+
|
593 |
+
def ramp_response_plot(system, slope=1, color='b', prec=8, lower_limit=0,
|
594 |
+
upper_limit=10, show_axes=False, grid=True, show=True, **kwargs):
|
595 |
+
r"""
|
596 |
+
Returns the ramp response of a continuous-time system.
|
597 |
+
|
598 |
+
Ramp function is defined as the straight line
|
599 |
+
passing through origin ($f(x) = mx$). The slope of
|
600 |
+
the ramp function can be varied by the user and
|
601 |
+
the default value is 1.
|
602 |
+
|
603 |
+
Parameters
|
604 |
+
==========
|
605 |
+
|
606 |
+
system : SISOLinearTimeInvariant type
|
607 |
+
The LTI SISO system for which the Ramp Response is to be computed.
|
608 |
+
slope : Number, optional
|
609 |
+
The slope of the input ramp function. Defaults to 1.
|
610 |
+
color : str, tuple, optional
|
611 |
+
The color of the line. Default is Blue.
|
612 |
+
show : boolean, optional
|
613 |
+
If ``True``, the plot will be displayed otherwise
|
614 |
+
the equivalent matplotlib ``plot`` object will be returned.
|
615 |
+
Defaults to True.
|
616 |
+
lower_limit : Number, optional
|
617 |
+
The lower limit of the plot range. Defaults to 0.
|
618 |
+
upper_limit : Number, optional
|
619 |
+
The upper limit of the plot range. Defaults to 10.
|
620 |
+
prec : int, optional
|
621 |
+
The decimal point precision for the point coordinate values.
|
622 |
+
Defaults to 8.
|
623 |
+
show_axes : boolean, optional
|
624 |
+
If ``True``, the coordinate axes will be shown. Defaults to False.
|
625 |
+
grid : boolean, optional
|
626 |
+
If ``True``, the plot will have a grid. Defaults to True.
|
627 |
+
|
628 |
+
Examples
|
629 |
+
========
|
630 |
+
|
631 |
+
.. plot::
|
632 |
+
:context: close-figs
|
633 |
+
:format: doctest
|
634 |
+
:include-source: True
|
635 |
+
|
636 |
+
>>> from sympy.abc import s
|
637 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
638 |
+
>>> from sympy.physics.control.control_plots import ramp_response_plot
|
639 |
+
>>> tf1 = TransferFunction(s, (s+4)*(s+8), s)
|
640 |
+
>>> ramp_response_plot(tf1, upper_limit=2) # doctest: +SKIP
|
641 |
+
|
642 |
+
See Also
|
643 |
+
========
|
644 |
+
|
645 |
+
step_response_plot, ramp_response_plot
|
646 |
+
|
647 |
+
References
|
648 |
+
==========
|
649 |
+
|
650 |
+
.. [1] https://en.wikipedia.org/wiki/Ramp_function
|
651 |
+
|
652 |
+
"""
|
653 |
+
x, y = ramp_response_numerical_data(system, slope=slope, prec=prec,
|
654 |
+
lower_limit=lower_limit, upper_limit=upper_limit, **kwargs)
|
655 |
+
plt.plot(x, y, color=color)
|
656 |
+
plt.xlabel('Time (s)')
|
657 |
+
plt.ylabel('Amplitude')
|
658 |
+
plt.title(f'Ramp Response of ${latex(system)}$ [Slope = {slope}]', pad=20)
|
659 |
+
|
660 |
+
if grid:
|
661 |
+
plt.grid()
|
662 |
+
if show_axes:
|
663 |
+
plt.axhline(0, color='black')
|
664 |
+
plt.axvline(0, color='black')
|
665 |
+
if show:
|
666 |
+
plt.show()
|
667 |
+
return
|
668 |
+
|
669 |
+
return plt
|
670 |
+
|
671 |
+
|
672 |
+
def bode_magnitude_numerical_data(system, initial_exp=-5, final_exp=5, freq_unit='rad/sec', **kwargs):
|
673 |
+
"""
|
674 |
+
Returns the numerical data of the Bode magnitude plot of the system.
|
675 |
+
It is internally used by ``bode_magnitude_plot`` to get the data
|
676 |
+
for plotting Bode magnitude plot. Users can use this data to further
|
677 |
+
analyse the dynamics of the system or plot using a different
|
678 |
+
backend/plotting-module.
|
679 |
+
|
680 |
+
Parameters
|
681 |
+
==========
|
682 |
+
|
683 |
+
system : SISOLinearTimeInvariant
|
684 |
+
The system for which the data is to be computed.
|
685 |
+
initial_exp : Number, optional
|
686 |
+
The initial exponent of 10 of the semilog plot. Defaults to -5.
|
687 |
+
final_exp : Number, optional
|
688 |
+
The final exponent of 10 of the semilog plot. Defaults to 5.
|
689 |
+
freq_unit : string, optional
|
690 |
+
User can choose between ``'rad/sec'`` (radians/second) and ``'Hz'`` (Hertz) as frequency units.
|
691 |
+
|
692 |
+
Returns
|
693 |
+
=======
|
694 |
+
|
695 |
+
tuple : (x, y)
|
696 |
+
x = x-axis values of the Bode magnitude plot.
|
697 |
+
y = y-axis values of the Bode magnitude plot.
|
698 |
+
|
699 |
+
Raises
|
700 |
+
======
|
701 |
+
|
702 |
+
NotImplementedError
|
703 |
+
When a SISO LTI system is not passed.
|
704 |
+
|
705 |
+
When time delay terms are present in the system.
|
706 |
+
|
707 |
+
ValueError
|
708 |
+
When more than one free symbol is present in the system.
|
709 |
+
The only variable in the transfer function should be
|
710 |
+
the variable of the Laplace transform.
|
711 |
+
|
712 |
+
When incorrect frequency units are given as input.
|
713 |
+
|
714 |
+
Examples
|
715 |
+
========
|
716 |
+
|
717 |
+
>>> from sympy.abc import s
|
718 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
719 |
+
>>> from sympy.physics.control.control_plots import bode_magnitude_numerical_data
|
720 |
+
>>> tf1 = TransferFunction(s**2 + 1, s**4 + 4*s**3 + 6*s**2 + 5*s + 2, s)
|
721 |
+
>>> bode_magnitude_numerical_data(tf1) # doctest: +SKIP
|
722 |
+
([1e-05, 1.5148378120533502e-05,..., 68437.36188804005, 100000.0],
|
723 |
+
[-6.020599914256786, -6.0205999155219505,..., -193.4117304087953, -200.00000000260573])
|
724 |
+
|
725 |
+
See Also
|
726 |
+
========
|
727 |
+
|
728 |
+
bode_magnitude_plot, bode_phase_numerical_data
|
729 |
+
|
730 |
+
"""
|
731 |
+
_check_system(system)
|
732 |
+
expr = system.to_expr()
|
733 |
+
freq_units = ('rad/sec', 'Hz')
|
734 |
+
if freq_unit not in freq_units:
|
735 |
+
raise ValueError('Only "rad/sec" and "Hz" are accepted frequency units.')
|
736 |
+
|
737 |
+
_w = Dummy("w", real=True)
|
738 |
+
if freq_unit == 'Hz':
|
739 |
+
repl = I*_w*2*pi
|
740 |
+
else:
|
741 |
+
repl = I*_w
|
742 |
+
w_expr = expr.subs({system.var: repl})
|
743 |
+
|
744 |
+
mag = 20*log(Abs(w_expr), 10)
|
745 |
+
|
746 |
+
x, y = LineOver1DRangeSeries(mag,
|
747 |
+
(_w, 10**initial_exp, 10**final_exp), xscale='log', **kwargs).get_points()
|
748 |
+
|
749 |
+
return x, y
|
750 |
+
|
751 |
+
|
752 |
+
def bode_magnitude_plot(system, initial_exp=-5, final_exp=5,
|
753 |
+
color='b', show_axes=False, grid=True, show=True, freq_unit='rad/sec', **kwargs):
|
754 |
+
r"""
|
755 |
+
Returns the Bode magnitude plot of a continuous-time system.
|
756 |
+
|
757 |
+
See ``bode_plot`` for all the parameters.
|
758 |
+
"""
|
759 |
+
x, y = bode_magnitude_numerical_data(system, initial_exp=initial_exp,
|
760 |
+
final_exp=final_exp, freq_unit=freq_unit)
|
761 |
+
plt.plot(x, y, color=color, **kwargs)
|
762 |
+
plt.xscale('log')
|
763 |
+
|
764 |
+
|
765 |
+
plt.xlabel('Frequency (%s) [Log Scale]' % freq_unit)
|
766 |
+
plt.ylabel('Magnitude (dB)')
|
767 |
+
plt.title(f'Bode Plot (Magnitude) of ${latex(system)}$', pad=20)
|
768 |
+
|
769 |
+
if grid:
|
770 |
+
plt.grid(True)
|
771 |
+
if show_axes:
|
772 |
+
plt.axhline(0, color='black')
|
773 |
+
plt.axvline(0, color='black')
|
774 |
+
if show:
|
775 |
+
plt.show()
|
776 |
+
return
|
777 |
+
|
778 |
+
return plt
|
779 |
+
|
780 |
+
|
781 |
+
def bode_phase_numerical_data(system, initial_exp=-5, final_exp=5, freq_unit='rad/sec', phase_unit='rad', **kwargs):
|
782 |
+
"""
|
783 |
+
Returns the numerical data of the Bode phase plot of the system.
|
784 |
+
It is internally used by ``bode_phase_plot`` to get the data
|
785 |
+
for plotting Bode phase plot. Users can use this data to further
|
786 |
+
analyse the dynamics of the system or plot using a different
|
787 |
+
backend/plotting-module.
|
788 |
+
|
789 |
+
Parameters
|
790 |
+
==========
|
791 |
+
|
792 |
+
system : SISOLinearTimeInvariant
|
793 |
+
The system for which the Bode phase plot data is to be computed.
|
794 |
+
initial_exp : Number, optional
|
795 |
+
The initial exponent of 10 of the semilog plot. Defaults to -5.
|
796 |
+
final_exp : Number, optional
|
797 |
+
The final exponent of 10 of the semilog plot. Defaults to 5.
|
798 |
+
freq_unit : string, optional
|
799 |
+
User can choose between ``'rad/sec'`` (radians/second) and '``'Hz'`` (Hertz) as frequency units.
|
800 |
+
phase_unit : string, optional
|
801 |
+
User can choose between ``'rad'`` (radians) and ``'deg'`` (degree) as phase units.
|
802 |
+
|
803 |
+
Returns
|
804 |
+
=======
|
805 |
+
|
806 |
+
tuple : (x, y)
|
807 |
+
x = x-axis values of the Bode phase plot.
|
808 |
+
y = y-axis values of the Bode phase plot.
|
809 |
+
|
810 |
+
Raises
|
811 |
+
======
|
812 |
+
|
813 |
+
NotImplementedError
|
814 |
+
When a SISO LTI system is not passed.
|
815 |
+
|
816 |
+
When time delay terms are present in the system.
|
817 |
+
|
818 |
+
ValueError
|
819 |
+
When more than one free symbol is present in the system.
|
820 |
+
The only variable in the transfer function should be
|
821 |
+
the variable of the Laplace transform.
|
822 |
+
|
823 |
+
When incorrect frequency or phase units are given as input.
|
824 |
+
|
825 |
+
Examples
|
826 |
+
========
|
827 |
+
|
828 |
+
>>> from sympy.abc import s
|
829 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
830 |
+
>>> from sympy.physics.control.control_plots import bode_phase_numerical_data
|
831 |
+
>>> tf1 = TransferFunction(s**2 + 1, s**4 + 4*s**3 + 6*s**2 + 5*s + 2, s)
|
832 |
+
>>> bode_phase_numerical_data(tf1) # doctest: +SKIP
|
833 |
+
([1e-05, 1.4472354033813751e-05, 2.035581932165858e-05,..., 47577.3248186011, 67884.09326036123, 100000.0],
|
834 |
+
[-2.5000000000291665e-05, -3.6180885085e-05, -5.08895483066e-05,...,-3.1415085799262523, -3.14155265358979])
|
835 |
+
|
836 |
+
See Also
|
837 |
+
========
|
838 |
+
|
839 |
+
bode_magnitude_plot, bode_phase_numerical_data
|
840 |
+
|
841 |
+
"""
|
842 |
+
_check_system(system)
|
843 |
+
expr = system.to_expr()
|
844 |
+
freq_units = ('rad/sec', 'Hz')
|
845 |
+
phase_units = ('rad', 'deg')
|
846 |
+
if freq_unit not in freq_units:
|
847 |
+
raise ValueError('Only "rad/sec" and "Hz" are accepted frequency units.')
|
848 |
+
if phase_unit not in phase_units:
|
849 |
+
raise ValueError('Only "rad" and "deg" are accepted phase units.')
|
850 |
+
|
851 |
+
_w = Dummy("w", real=True)
|
852 |
+
if freq_unit == 'Hz':
|
853 |
+
repl = I*_w*2*pi
|
854 |
+
else:
|
855 |
+
repl = I*_w
|
856 |
+
w_expr = expr.subs({system.var: repl})
|
857 |
+
|
858 |
+
if phase_unit == 'deg':
|
859 |
+
phase = arg(w_expr)*180/pi
|
860 |
+
else:
|
861 |
+
phase = arg(w_expr)
|
862 |
+
|
863 |
+
x, y = LineOver1DRangeSeries(phase,
|
864 |
+
(_w, 10**initial_exp, 10**final_exp), xscale='log', **kwargs).get_points()
|
865 |
+
|
866 |
+
return x, y
|
867 |
+
|
868 |
+
|
869 |
+
def bode_phase_plot(system, initial_exp=-5, final_exp=5,
|
870 |
+
color='b', show_axes=False, grid=True, show=True, freq_unit='rad/sec', phase_unit='rad', **kwargs):
|
871 |
+
r"""
|
872 |
+
Returns the Bode phase plot of a continuous-time system.
|
873 |
+
|
874 |
+
See ``bode_plot`` for all the parameters.
|
875 |
+
"""
|
876 |
+
x, y = bode_phase_numerical_data(system, initial_exp=initial_exp,
|
877 |
+
final_exp=final_exp, freq_unit=freq_unit, phase_unit=phase_unit)
|
878 |
+
plt.plot(x, y, color=color, **kwargs)
|
879 |
+
plt.xscale('log')
|
880 |
+
|
881 |
+
plt.xlabel('Frequency (%s) [Log Scale]' % freq_unit)
|
882 |
+
plt.ylabel('Phase (%s)' % phase_unit)
|
883 |
+
plt.title(f'Bode Plot (Phase) of ${latex(system)}$', pad=20)
|
884 |
+
|
885 |
+
if grid:
|
886 |
+
plt.grid(True)
|
887 |
+
if show_axes:
|
888 |
+
plt.axhline(0, color='black')
|
889 |
+
plt.axvline(0, color='black')
|
890 |
+
if show:
|
891 |
+
plt.show()
|
892 |
+
return
|
893 |
+
|
894 |
+
return plt
|
895 |
+
|
896 |
+
|
897 |
+
def bode_plot(system, initial_exp=-5, final_exp=5,
|
898 |
+
grid=True, show_axes=False, show=True, freq_unit='rad/sec', phase_unit='rad', **kwargs):
|
899 |
+
r"""
|
900 |
+
Returns the Bode phase and magnitude plots of a continuous-time system.
|
901 |
+
|
902 |
+
Parameters
|
903 |
+
==========
|
904 |
+
|
905 |
+
system : SISOLinearTimeInvariant type
|
906 |
+
The LTI SISO system for which the Bode Plot is to be computed.
|
907 |
+
initial_exp : Number, optional
|
908 |
+
The initial exponent of 10 of the semilog plot. Defaults to -5.
|
909 |
+
final_exp : Number, optional
|
910 |
+
The final exponent of 10 of the semilog plot. Defaults to 5.
|
911 |
+
show : boolean, optional
|
912 |
+
If ``True``, the plot will be displayed otherwise
|
913 |
+
the equivalent matplotlib ``plot`` object will be returned.
|
914 |
+
Defaults to True.
|
915 |
+
prec : int, optional
|
916 |
+
The decimal point precision for the point coordinate values.
|
917 |
+
Defaults to 8.
|
918 |
+
grid : boolean, optional
|
919 |
+
If ``True``, the plot will have a grid. Defaults to True.
|
920 |
+
show_axes : boolean, optional
|
921 |
+
If ``True``, the coordinate axes will be shown. Defaults to False.
|
922 |
+
freq_unit : string, optional
|
923 |
+
User can choose between ``'rad/sec'`` (radians/second) and ``'Hz'`` (Hertz) as frequency units.
|
924 |
+
phase_unit : string, optional
|
925 |
+
User can choose between ``'rad'`` (radians) and ``'deg'`` (degree) as phase units.
|
926 |
+
|
927 |
+
Examples
|
928 |
+
========
|
929 |
+
|
930 |
+
.. plot::
|
931 |
+
:context: close-figs
|
932 |
+
:format: doctest
|
933 |
+
:include-source: True
|
934 |
+
|
935 |
+
>>> from sympy.abc import s
|
936 |
+
>>> from sympy.physics.control.lti import TransferFunction
|
937 |
+
>>> from sympy.physics.control.control_plots import bode_plot
|
938 |
+
>>> tf1 = TransferFunction(1*s**2 + 0.1*s + 7.5, 1*s**4 + 0.12*s**3 + 9*s**2, s)
|
939 |
+
>>> bode_plot(tf1, initial_exp=0.2, final_exp=0.7) # doctest: +SKIP
|
940 |
+
|
941 |
+
See Also
|
942 |
+
========
|
943 |
+
|
944 |
+
bode_magnitude_plot, bode_phase_plot
|
945 |
+
|
946 |
+
"""
|
947 |
+
plt.subplot(211)
|
948 |
+
mag = bode_magnitude_plot(system, initial_exp=initial_exp, final_exp=final_exp,
|
949 |
+
show=False, grid=grid, show_axes=show_axes,
|
950 |
+
freq_unit=freq_unit, **kwargs)
|
951 |
+
mag.title(f'Bode Plot of ${latex(system)}$', pad=20)
|
952 |
+
mag.xlabel(None)
|
953 |
+
plt.subplot(212)
|
954 |
+
bode_phase_plot(system, initial_exp=initial_exp, final_exp=final_exp,
|
955 |
+
show=False, grid=grid, show_axes=show_axes, freq_unit=freq_unit, phase_unit=phase_unit, **kwargs).title(None)
|
956 |
+
|
957 |
+
if show:
|
958 |
+
plt.show()
|
959 |
+
return
|
960 |
+
|
961 |
+
return plt
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/lti.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (200 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__pycache__/test_control_plots.cpython-310.pyc
ADDED
Binary file (11.6 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/__pycache__/test_lti.cpython-310.pyc
ADDED
Binary file (53.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/control/tests/test_control_plots.py
ADDED
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from math import isclose
|
2 |
+
from sympy.core.numbers import I
|
3 |
+
from sympy.core.symbol import Dummy
|
4 |
+
from sympy.functions.elementary.complexes import (Abs, arg)
|
5 |
+
from sympy.functions.elementary.exponential import log
|
6 |
+
from sympy.abc import s, p, a
|
7 |
+
from sympy.external import import_module
|
8 |
+
from sympy.physics.control.control_plots import \
|
9 |
+
(pole_zero_numerical_data, pole_zero_plot, step_response_numerical_data,
|
10 |
+
step_response_plot, impulse_response_numerical_data,
|
11 |
+
impulse_response_plot, ramp_response_numerical_data,
|
12 |
+
ramp_response_plot, bode_magnitude_numerical_data,
|
13 |
+
bode_phase_numerical_data, bode_plot)
|
14 |
+
from sympy.physics.control.lti import (TransferFunction,
|
15 |
+
Series, Parallel, TransferFunctionMatrix)
|
16 |
+
from sympy.testing.pytest import raises, skip
|
17 |
+
|
18 |
+
matplotlib = import_module(
|
19 |
+
'matplotlib', import_kwargs={'fromlist': ['pyplot']},
|
20 |
+
catch=(RuntimeError,))
|
21 |
+
|
22 |
+
numpy = import_module('numpy')
|
23 |
+
|
24 |
+
tf1 = TransferFunction(1, p**2 + 0.5*p + 2, p)
|
25 |
+
tf2 = TransferFunction(p, 6*p**2 + 3*p + 1, p)
|
26 |
+
tf3 = TransferFunction(p, p**3 - 1, p)
|
27 |
+
tf4 = TransferFunction(10, p**3, p)
|
28 |
+
tf5 = TransferFunction(5, s**2 + 2*s + 10, s)
|
29 |
+
tf6 = TransferFunction(1, 1, s)
|
30 |
+
tf7 = TransferFunction(4*s*3 + 9*s**2 + 0.1*s + 11, 8*s**6 + 9*s**4 + 11, s)
|
31 |
+
tf8 = TransferFunction(5, s**2 + (2+I)*s + 10, s)
|
32 |
+
|
33 |
+
ser1 = Series(tf4, TransferFunction(1, p - 5, p))
|
34 |
+
ser2 = Series(tf3, TransferFunction(p, p + 2, p))
|
35 |
+
|
36 |
+
par1 = Parallel(tf1, tf2)
|
37 |
+
par2 = Parallel(tf1, tf2, tf3)
|
38 |
+
|
39 |
+
|
40 |
+
def _to_tuple(a, b):
|
41 |
+
return tuple(a), tuple(b)
|
42 |
+
|
43 |
+
def _trim_tuple(a, b):
|
44 |
+
a, b = _to_tuple(a, b)
|
45 |
+
return tuple(a[0: 2] + a[len(a)//2 : len(a)//2 + 1] + a[-2:]), \
|
46 |
+
tuple(b[0: 2] + b[len(b)//2 : len(b)//2 + 1] + b[-2:])
|
47 |
+
|
48 |
+
def y_coordinate_equality(plot_data_func, evalf_func, system):
|
49 |
+
"""Checks whether the y-coordinate value of the plotted
|
50 |
+
data point is equal to the value of the function at a
|
51 |
+
particular x."""
|
52 |
+
x, y = plot_data_func(system)
|
53 |
+
x, y = _trim_tuple(x, y)
|
54 |
+
y_exp = tuple(evalf_func(system, x_i) for x_i in x)
|
55 |
+
return all(Abs(y_exp_i - y_i) < 1e-8 for y_exp_i, y_i in zip(y_exp, y))
|
56 |
+
|
57 |
+
|
58 |
+
def test_errors():
|
59 |
+
if not matplotlib:
|
60 |
+
skip("Matplotlib not the default backend")
|
61 |
+
|
62 |
+
# Invalid `system` check
|
63 |
+
tfm = TransferFunctionMatrix([[tf6, tf5], [tf5, tf6]])
|
64 |
+
expr = 1/(s**2 - 1)
|
65 |
+
raises(NotImplementedError, lambda: pole_zero_plot(tfm))
|
66 |
+
raises(NotImplementedError, lambda: pole_zero_numerical_data(expr))
|
67 |
+
raises(NotImplementedError, lambda: impulse_response_plot(expr))
|
68 |
+
raises(NotImplementedError, lambda: impulse_response_numerical_data(tfm))
|
69 |
+
raises(NotImplementedError, lambda: step_response_plot(tfm))
|
70 |
+
raises(NotImplementedError, lambda: step_response_numerical_data(expr))
|
71 |
+
raises(NotImplementedError, lambda: ramp_response_plot(expr))
|
72 |
+
raises(NotImplementedError, lambda: ramp_response_numerical_data(tfm))
|
73 |
+
raises(NotImplementedError, lambda: bode_plot(tfm))
|
74 |
+
|
75 |
+
# More than 1 variables
|
76 |
+
tf_a = TransferFunction(a, s + 1, s)
|
77 |
+
raises(ValueError, lambda: pole_zero_plot(tf_a))
|
78 |
+
raises(ValueError, lambda: pole_zero_numerical_data(tf_a))
|
79 |
+
raises(ValueError, lambda: impulse_response_plot(tf_a))
|
80 |
+
raises(ValueError, lambda: impulse_response_numerical_data(tf_a))
|
81 |
+
raises(ValueError, lambda: step_response_plot(tf_a))
|
82 |
+
raises(ValueError, lambda: step_response_numerical_data(tf_a))
|
83 |
+
raises(ValueError, lambda: ramp_response_plot(tf_a))
|
84 |
+
raises(ValueError, lambda: ramp_response_numerical_data(tf_a))
|
85 |
+
raises(ValueError, lambda: bode_plot(tf_a))
|
86 |
+
|
87 |
+
# lower_limit > 0 for response plots
|
88 |
+
raises(ValueError, lambda: impulse_response_plot(tf1, lower_limit=-1))
|
89 |
+
raises(ValueError, lambda: step_response_plot(tf1, lower_limit=-0.1))
|
90 |
+
raises(ValueError, lambda: ramp_response_plot(tf1, lower_limit=-4/3))
|
91 |
+
|
92 |
+
# slope in ramp_response_plot() is negative
|
93 |
+
raises(ValueError, lambda: ramp_response_plot(tf1, slope=-0.1))
|
94 |
+
|
95 |
+
# incorrect frequency or phase unit
|
96 |
+
raises(ValueError, lambda: bode_plot(tf1,freq_unit = 'hz'))
|
97 |
+
raises(ValueError, lambda: bode_plot(tf1,phase_unit = 'degree'))
|
98 |
+
|
99 |
+
|
100 |
+
def test_pole_zero():
|
101 |
+
if not numpy:
|
102 |
+
skip("NumPy is required for this test")
|
103 |
+
|
104 |
+
def pz_tester(sys, expected_value):
|
105 |
+
z, p = pole_zero_numerical_data(sys)
|
106 |
+
z_check = numpy.allclose(z, expected_value[0])
|
107 |
+
p_check = numpy.allclose(p, expected_value[1])
|
108 |
+
return p_check and z_check
|
109 |
+
|
110 |
+
exp1 = [[], [-0.24999999999999994+1.3919410907075054j, -0.24999999999999994-1.3919410907075054j]]
|
111 |
+
exp2 = [[0.0], [-0.25+0.3227486121839514j, -0.25-0.3227486121839514j]]
|
112 |
+
exp3 = [[0.0], [-0.5000000000000004+0.8660254037844395j,
|
113 |
+
-0.5000000000000004-0.8660254037844395j, 0.9999999999999998+0j]]
|
114 |
+
exp4 = [[], [5.0, 0.0, 0.0, 0.0]]
|
115 |
+
exp5 = [[-5.645751311064592, -0.5000000000000008, -0.3542486889354093],
|
116 |
+
[-0.24999999999999986+1.3919410907075052j,
|
117 |
+
-0.24999999999999986-1.3919410907075052j, -0.2499999999999998+0.32274861218395134j,
|
118 |
+
-0.2499999999999998-0.32274861218395134j]]
|
119 |
+
exp6 = [[], [-1.1641600331447917-3.545808351896439j,
|
120 |
+
-0.8358399668552097+2.5458083518964383j]]
|
121 |
+
|
122 |
+
assert pz_tester(tf1, exp1)
|
123 |
+
assert pz_tester(tf2, exp2)
|
124 |
+
assert pz_tester(tf3, exp3)
|
125 |
+
assert pz_tester(ser1, exp4)
|
126 |
+
assert pz_tester(par1, exp5)
|
127 |
+
assert pz_tester(tf8, exp6)
|
128 |
+
|
129 |
+
|
130 |
+
def test_bode():
|
131 |
+
if not numpy:
|
132 |
+
skip("NumPy is required for this test")
|
133 |
+
|
134 |
+
def bode_phase_evalf(system, point):
|
135 |
+
expr = system.to_expr()
|
136 |
+
_w = Dummy("w", real=True)
|
137 |
+
w_expr = expr.subs({system.var: I*_w})
|
138 |
+
return arg(w_expr).subs({_w: point}).evalf()
|
139 |
+
|
140 |
+
def bode_mag_evalf(system, point):
|
141 |
+
expr = system.to_expr()
|
142 |
+
_w = Dummy("w", real=True)
|
143 |
+
w_expr = expr.subs({system.var: I*_w})
|
144 |
+
return 20*log(Abs(w_expr), 10).subs({_w: point}).evalf()
|
145 |
+
|
146 |
+
def test_bode_data(sys):
|
147 |
+
return y_coordinate_equality(bode_magnitude_numerical_data, bode_mag_evalf, sys) \
|
148 |
+
and y_coordinate_equality(bode_phase_numerical_data, bode_phase_evalf, sys)
|
149 |
+
|
150 |
+
assert test_bode_data(tf1)
|
151 |
+
assert test_bode_data(tf2)
|
152 |
+
assert test_bode_data(tf3)
|
153 |
+
assert test_bode_data(tf4)
|
154 |
+
assert test_bode_data(tf5)
|
155 |
+
|
156 |
+
|
157 |
+
def check_point_accuracy(a, b):
|
158 |
+
return all(isclose(a_i, b_i, rel_tol=10e-12) for \
|
159 |
+
a_i, b_i in zip(a, b))
|
160 |
+
|
161 |
+
|
162 |
+
def test_impulse_response():
|
163 |
+
if not numpy:
|
164 |
+
skip("NumPy is required for this test")
|
165 |
+
|
166 |
+
def impulse_res_tester(sys, expected_value):
|
167 |
+
x, y = _to_tuple(*impulse_response_numerical_data(sys,
|
168 |
+
adaptive=False, nb_of_points=10))
|
169 |
+
x_check = check_point_accuracy(x, expected_value[0])
|
170 |
+
y_check = check_point_accuracy(y, expected_value[1])
|
171 |
+
return x_check and y_check
|
172 |
+
|
173 |
+
exp1 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
174 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
175 |
+
(0.0, 0.544019738507865, 0.01993849743234938, -0.31140243360893216, -0.022852779906491996, 0.1778306498155759,
|
176 |
+
0.01962941084328499, -0.1013115194573652, -0.014975541213105696, 0.0575789724730714))
|
177 |
+
exp2 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
178 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (0.1666666675, 0.08389223412935855,
|
179 |
+
0.02338051973475047, -0.014966807776379383, -0.034645954223054234, -0.040560075735512804,
|
180 |
+
-0.037658628907103885, -0.030149507719590022, -0.021162090730736834, -0.012721292737437523))
|
181 |
+
exp3 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
182 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (4.369893391586999e-09, 1.1750333000630964,
|
183 |
+
3.2922404058312473, 9.432290008148343, 28.37098083007151, 86.18577464367974, 261.90356653762115,
|
184 |
+
795.6538758627842, 2416.9920942096983, 7342.159505206647))
|
185 |
+
exp4 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
186 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (0.0, 6.17283950617284, 24.69135802469136,
|
187 |
+
55.555555555555564, 98.76543209876544, 154.320987654321, 222.22222222222226, 302.46913580246917,
|
188 |
+
395.0617283950618, 500.0))
|
189 |
+
exp5 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
190 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (0.0, -0.10455606138085417,
|
191 |
+
0.06757671513476461, -0.03234567568833768, 0.013582514927757873, -0.005273419510705473,
|
192 |
+
0.0019364083003354075, -0.000680070134067832, 0.00022969845960406913, -7.476094359583917e-05))
|
193 |
+
exp6 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
194 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
195 |
+
(-6.016699583000218e-09, 0.35039802056107394, 3.3728423827689884, 12.119846079276684,
|
196 |
+
25.86101014293389, 29.352480635282088, -30.49475907497664, -273.8717189554019, -863.2381702029659,
|
197 |
+
-1747.0262164682233))
|
198 |
+
exp7 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335,
|
199 |
+
4.444444444444445, 5.555555555555555, 6.666666666666667, 7.777777777777779,
|
200 |
+
8.88888888888889, 10.0), (0.0, 18.934638095560974, 5346.93244680907, 1384609.8718249386,
|
201 |
+
358161126.65801865, 92645770015.70108, 23964739753087.42, 6198974342083139.0, 1.603492601616059e+18,
|
202 |
+
4.147764422869658e+20))
|
203 |
+
|
204 |
+
assert impulse_res_tester(tf1, exp1)
|
205 |
+
assert impulse_res_tester(tf2, exp2)
|
206 |
+
assert impulse_res_tester(tf3, exp3)
|
207 |
+
assert impulse_res_tester(tf4, exp4)
|
208 |
+
assert impulse_res_tester(tf5, exp5)
|
209 |
+
assert impulse_res_tester(tf7, exp6)
|
210 |
+
assert impulse_res_tester(ser1, exp7)
|
211 |
+
|
212 |
+
|
213 |
+
def test_step_response():
|
214 |
+
if not numpy:
|
215 |
+
skip("NumPy is required for this test")
|
216 |
+
|
217 |
+
def step_res_tester(sys, expected_value):
|
218 |
+
x, y = _to_tuple(*step_response_numerical_data(sys,
|
219 |
+
adaptive=False, nb_of_points=10))
|
220 |
+
x_check = check_point_accuracy(x, expected_value[0])
|
221 |
+
y_check = check_point_accuracy(y, expected_value[1])
|
222 |
+
return x_check and y_check
|
223 |
+
|
224 |
+
exp1 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
225 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
226 |
+
(-1.9193285738516863e-08, 0.42283495488246126, 0.7840485977945262, 0.5546841805655717,
|
227 |
+
0.33903033806932087, 0.4627251747410237, 0.5909907598988051, 0.5247213989553071,
|
228 |
+
0.4486997874319281, 0.4839358435839171))
|
229 |
+
exp2 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
230 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
231 |
+
(0.0, 0.13728409095645816, 0.19474559355325086, 0.1974909129243011, 0.16841657696573073,
|
232 |
+
0.12559777736159378, 0.08153828016664713, 0.04360471317348958, 0.015072994568868221,
|
233 |
+
-0.003636420058445484))
|
234 |
+
exp3 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
235 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
236 |
+
(0.0, 0.6314542141914303, 2.9356520038101035, 9.37731009663807, 28.452300356688376,
|
237 |
+
86.25721933273988, 261.9236645044672, 795.6435410577224, 2416.9786984578764, 7342.154119725917))
|
238 |
+
exp4 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
239 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
240 |
+
(0.0, 2.286236899862826, 18.28989519890261, 61.72839629629631, 146.31916159122088, 285.7796124828532,
|
241 |
+
493.8271703703705, 784.1792566529494, 1170.553292729767, 1666.6667))
|
242 |
+
exp5 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
243 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
244 |
+
(-3.999999997894577e-09, 0.6720357068882895, 0.4429938256137113, 0.5182010838004518,
|
245 |
+
0.4944139147159695, 0.5016379853883338, 0.4995466896527733, 0.5001154784851325,
|
246 |
+
0.49997448824584123, 0.5000039745919259))
|
247 |
+
exp6 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
248 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
249 |
+
(-1.5433688493882158e-09, 0.3428705539937336, 1.1253619102202777, 3.1849962651016517,
|
250 |
+
9.47532757182671, 28.727231099148135, 87.29426924860557, 265.2138681048606, 805.6636260007757,
|
251 |
+
2447.387582370878))
|
252 |
+
|
253 |
+
assert step_res_tester(tf1, exp1)
|
254 |
+
assert step_res_tester(tf2, exp2)
|
255 |
+
assert step_res_tester(tf3, exp3)
|
256 |
+
assert step_res_tester(tf4, exp4)
|
257 |
+
assert step_res_tester(tf5, exp5)
|
258 |
+
assert step_res_tester(ser2, exp6)
|
259 |
+
|
260 |
+
|
261 |
+
def test_ramp_response():
|
262 |
+
if not numpy:
|
263 |
+
skip("NumPy is required for this test")
|
264 |
+
|
265 |
+
def ramp_res_tester(sys, num_points, expected_value, slope=1):
|
266 |
+
x, y = _to_tuple(*ramp_response_numerical_data(sys,
|
267 |
+
slope=slope, adaptive=False, nb_of_points=num_points))
|
268 |
+
x_check = check_point_accuracy(x, expected_value[0])
|
269 |
+
y_check = check_point_accuracy(y, expected_value[1])
|
270 |
+
return x_check and y_check
|
271 |
+
|
272 |
+
exp1 = ((0.0, 2.0, 4.0, 6.0, 8.0, 10.0), (0.0, 0.7324667795033895, 1.9909720978650398,
|
273 |
+
2.7956587704217783, 3.9224897567931514, 4.85022655284895))
|
274 |
+
exp2 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445,
|
275 |
+
5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0),
|
276 |
+
(2.4360213402019326e-08, 0.10175320182493253, 0.33057612497658406, 0.5967937263298935,
|
277 |
+
0.8431511866718248, 1.0398805391471613, 1.1776043125035738, 1.2600994825747305, 1.2981042689274653,
|
278 |
+
1.304684417610106))
|
279 |
+
exp3 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
280 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (-3.9329040468771836e-08,
|
281 |
+
0.34686634635794555, 2.9998828170537903, 12.33303690737476, 40.993913948137795, 127.84145222317912,
|
282 |
+
391.41713691996, 1192.0006858708389, 3623.9808672503405, 11011.728034546572))
|
283 |
+
exp4 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
284 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (0.0, 1.9051973784484078, 30.483158055174524,
|
285 |
+
154.32098765432104, 487.7305288827924, 1190.7483615302544, 2469.1358024691367, 4574.3789056546275,
|
286 |
+
7803.688462124678, 12500.0))
|
287 |
+
exp5 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
288 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (0.0, 3.8844361856975635, 9.141792069209865,
|
289 |
+
14.096349157657231, 19.09783068994694, 24.10179770390321, 29.09907319114121, 34.10040420185154,
|
290 |
+
39.09983919254265, 44.10006013058409))
|
291 |
+
exp6 = ((0.0, 1.1111111111111112, 2.2222222222222223, 3.3333333333333335, 4.444444444444445, 5.555555555555555,
|
292 |
+
6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0), (0.0, 1.1111111111111112, 2.2222222222222223,
|
293 |
+
3.3333333333333335, 4.444444444444445, 5.555555555555555, 6.666666666666667, 7.777777777777779, 8.88888888888889, 10.0))
|
294 |
+
|
295 |
+
assert ramp_res_tester(tf1, 6, exp1)
|
296 |
+
assert ramp_res_tester(tf2, 10, exp2, 1.2)
|
297 |
+
assert ramp_res_tester(tf3, 10, exp3, 1.5)
|
298 |
+
assert ramp_res_tester(tf4, 10, exp4, 3)
|
299 |
+
assert ramp_res_tester(tf5, 10, exp5, 9)
|
300 |
+
assert ramp_res_tester(tf6, 10, exp6)
|
llmeval-env/lib/python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ational, oo)
|
5 |
+
from sympy.core.power import Pow
|
6 |
+
from sympy.core.singleton import S
|
7 |
+
from sympy.core.symbol import symbols
|
8 |
+
from sympy.functions.elementary.exponential import exp
|
9 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
10 |
+
from sympy.matrices.dense import eye
|
11 |
+
from sympy.polys.polytools import factor
|
12 |
+
from sympy.polys.rootoftools import CRootOf
|
13 |
+
from sympy.simplify.simplify import simplify
|
14 |
+
from sympy.core.containers import Tuple
|
15 |
+
from sympy.matrices import ImmutableMatrix, Matrix
|
16 |
+
from sympy.physics.control import (TransferFunction, Series, Parallel,
|
17 |
+
Feedback, TransferFunctionMatrix, MIMOSeries, MIMOParallel, MIMOFeedback,
|
18 |
+
bilinear, backward_diff)
|
19 |
+
from sympy.testing.pytest import raises
|
20 |
+
|
21 |
+
a, x, b, s, g, d, p, k, a0, a1, a2, b0, b1, b2, tau, zeta, wn, T = symbols('a, x, b, s, g, d, p, k,\
|
22 |
+
a0:3, b0:3, tau, zeta, wn, T')
|
23 |
+
TF1 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
24 |
+
TF2 = TransferFunction(k, 1, s)
|
25 |
+
TF3 = TransferFunction(a2*p - s, a2*s + p, s)
|
26 |
+
|
27 |
+
|
28 |
+
def test_TransferFunction_construction():
|
29 |
+
tf = TransferFunction(s + 1, s**2 + s + 1, s)
|
30 |
+
assert tf.num == (s + 1)
|
31 |
+
assert tf.den == (s**2 + s + 1)
|
32 |
+
assert tf.args == (s + 1, s**2 + s + 1, s)
|
33 |
+
|
34 |
+
tf1 = TransferFunction(s + 4, s - 5, s)
|
35 |
+
assert tf1.num == (s + 4)
|
36 |
+
assert tf1.den == (s - 5)
|
37 |
+
assert tf1.args == (s + 4, s - 5, s)
|
38 |
+
|
39 |
+
# using different polynomial variables.
|
40 |
+
tf2 = TransferFunction(p + 3, p**2 - 9, p)
|
41 |
+
assert tf2.num == (p + 3)
|
42 |
+
assert tf2.den == (p**2 - 9)
|
43 |
+
assert tf2.args == (p + 3, p**2 - 9, p)
|
44 |
+
|
45 |
+
tf3 = TransferFunction(p**3 + 5*p**2 + 4, p**4 + 3*p + 1, p)
|
46 |
+
assert tf3.args == (p**3 + 5*p**2 + 4, p**4 + 3*p + 1, p)
|
47 |
+
|
48 |
+
# no pole-zero cancellation on its own.
|
49 |
+
tf4 = TransferFunction((s + 3)*(s - 1), (s - 1)*(s + 5), s)
|
50 |
+
assert tf4.den == (s - 1)*(s + 5)
|
51 |
+
assert tf4.args == ((s + 3)*(s - 1), (s - 1)*(s + 5), s)
|
52 |
+
|
53 |
+
tf4_ = TransferFunction(p + 2, p + 2, p)
|
54 |
+
assert tf4_.args == (p + 2, p + 2, p)
|
55 |
+
|
56 |
+
tf5 = TransferFunction(s - 1, 4 - p, s)
|
57 |
+
assert tf5.args == (s - 1, 4 - p, s)
|
58 |
+
|
59 |
+
tf5_ = TransferFunction(s - 1, s - 1, s)
|
60 |
+
assert tf5_.args == (s - 1, s - 1, s)
|
61 |
+
|
62 |
+
tf6 = TransferFunction(5, 6, s)
|
63 |
+
assert tf6.num == 5
|
64 |
+
assert tf6.den == 6
|
65 |
+
assert tf6.args == (5, 6, s)
|
66 |
+
|
67 |
+
tf6_ = TransferFunction(1/2, 4, s)
|
68 |
+
assert tf6_.num == 0.5
|
69 |
+
assert tf6_.den == 4
|
70 |
+
assert tf6_.args == (0.500000000000000, 4, s)
|
71 |
+
|
72 |
+
tf7 = TransferFunction(3*s**2 + 2*p + 4*s, 8*p**2 + 7*s, s)
|
73 |
+
tf8 = TransferFunction(3*s**2 + 2*p + 4*s, 8*p**2 + 7*s, p)
|
74 |
+
assert not tf7 == tf8
|
75 |
+
|
76 |
+
tf7_ = TransferFunction(a0*s + a1*s**2 + a2*s**3, b0*p - b1*s, s)
|
77 |
+
tf8_ = TransferFunction(a0*s + a1*s**2 + a2*s**3, b0*p - b1*s, s)
|
78 |
+
assert tf7_ == tf8_
|
79 |
+
assert -(-tf7_) == tf7_ == -(-(-(-tf7_)))
|
80 |
+
|
81 |
+
tf9 = TransferFunction(a*s**3 + b*s**2 + g*s + d, d*p + g*p**2 + g*s, s)
|
82 |
+
assert tf9.args == (a*s**3 + b*s**2 + d + g*s, d*p + g*p**2 + g*s, s)
|
83 |
+
|
84 |
+
tf10 = TransferFunction(p**3 + d, g*s**2 + d*s + a, p)
|
85 |
+
tf10_ = TransferFunction(p**3 + d, g*s**2 + d*s + a, p)
|
86 |
+
assert tf10.args == (d + p**3, a + d*s + g*s**2, p)
|
87 |
+
assert tf10_ == tf10
|
88 |
+
|
89 |
+
tf11 = TransferFunction(a1*s + a0, b2*s**2 + b1*s + b0, s)
|
90 |
+
assert tf11.num == (a0 + a1*s)
|
91 |
+
assert tf11.den == (b0 + b1*s + b2*s**2)
|
92 |
+
assert tf11.args == (a0 + a1*s, b0 + b1*s + b2*s**2, s)
|
93 |
+
|
94 |
+
# when just the numerator is 0, leave the denominator alone.
|
95 |
+
tf12 = TransferFunction(0, p**2 - p + 1, p)
|
96 |
+
assert tf12.args == (0, p**2 - p + 1, p)
|
97 |
+
|
98 |
+
tf13 = TransferFunction(0, 1, s)
|
99 |
+
assert tf13.args == (0, 1, s)
|
100 |
+
|
101 |
+
# float exponents
|
102 |
+
tf14 = TransferFunction(a0*s**0.5 + a2*s**0.6 - a1, a1*p**(-8.7), s)
|
103 |
+
assert tf14.args == (a0*s**0.5 - a1 + a2*s**0.6, a1*p**(-8.7), s)
|
104 |
+
|
105 |
+
tf15 = TransferFunction(a2**2*p**(1/4) + a1*s**(-4/5), a0*s - p, p)
|
106 |
+
assert tf15.args == (a1*s**(-0.8) + a2**2*p**0.25, a0*s - p, p)
|
107 |
+
|
108 |
+
omega_o, k_p, k_o, k_i = symbols('omega_o, k_p, k_o, k_i')
|
109 |
+
tf18 = TransferFunction((k_p + k_o*s + k_i/s), s**2 + 2*omega_o*s + omega_o**2, s)
|
110 |
+
assert tf18.num == k_i/s + k_o*s + k_p
|
111 |
+
assert tf18.args == (k_i/s + k_o*s + k_p, omega_o**2 + 2*omega_o*s + s**2, s)
|
112 |
+
|
113 |
+
# ValueError when denominator is zero.
|
114 |
+
raises(ValueError, lambda: TransferFunction(4, 0, s))
|
115 |
+
raises(ValueError, lambda: TransferFunction(s, 0, s))
|
116 |
+
raises(ValueError, lambda: TransferFunction(0, 0, s))
|
117 |
+
|
118 |
+
raises(TypeError, lambda: TransferFunction(Matrix([1, 2, 3]), s, s))
|
119 |
+
|
120 |
+
raises(TypeError, lambda: TransferFunction(s**2 + 2*s - 1, s + 3, 3))
|
121 |
+
raises(TypeError, lambda: TransferFunction(p + 1, 5 - p, 4))
|
122 |
+
raises(TypeError, lambda: TransferFunction(3, 4, 8))
|
123 |
+
|
124 |
+
|
125 |
+
def test_TransferFunction_functions():
|
126 |
+
# classmethod from_rational_expression
|
127 |
+
expr_1 = Mul(0, Pow(s, -1, evaluate=False), evaluate=False)
|
128 |
+
expr_2 = s/0
|
129 |
+
expr_3 = (p*s**2 + 5*s)/(s + 1)**3
|
130 |
+
expr_4 = 6
|
131 |
+
expr_5 = ((2 + 3*s)*(5 + 2*s))/((9 + 3*s)*(5 + 2*s**2))
|
132 |
+
expr_6 = (9*s**4 + 4*s**2 + 8)/((s + 1)*(s + 9))
|
133 |
+
tf = TransferFunction(s + 1, s**2 + 2, s)
|
134 |
+
delay = exp(-s/tau)
|
135 |
+
expr_7 = delay*tf.to_expr()
|
136 |
+
H1 = TransferFunction.from_rational_expression(expr_7, s)
|
137 |
+
H2 = TransferFunction(s + 1, (s**2 + 2)*exp(s/tau), s)
|
138 |
+
expr_8 = Add(2, 3*s/(s**2 + 1), evaluate=False)
|
139 |
+
|
140 |
+
assert TransferFunction.from_rational_expression(expr_1) == TransferFunction(0, s, s)
|
141 |
+
raises(ZeroDivisionError, lambda: TransferFunction.from_rational_expression(expr_2))
|
142 |
+
raises(ValueError, lambda: TransferFunction.from_rational_expression(expr_3))
|
143 |
+
assert TransferFunction.from_rational_expression(expr_3, s) == TransferFunction((p*s**2 + 5*s), (s + 1)**3, s)
|
144 |
+
assert TransferFunction.from_rational_expression(expr_3, p) == TransferFunction((p*s**2 + 5*s), (s + 1)**3, p)
|
145 |
+
raises(ValueError, lambda: TransferFunction.from_rational_expression(expr_4))
|
146 |
+
assert TransferFunction.from_rational_expression(expr_4, s) == TransferFunction(6, 1, s)
|
147 |
+
assert TransferFunction.from_rational_expression(expr_5, s) == \
|
148 |
+
TransferFunction((2 + 3*s)*(5 + 2*s), (9 + 3*s)*(5 + 2*s**2), s)
|
149 |
+
assert TransferFunction.from_rational_expression(expr_6, s) == \
|
150 |
+
TransferFunction((9*s**4 + 4*s**2 + 8), (s + 1)*(s + 9), s)
|
151 |
+
assert H1 == H2
|
152 |
+
assert TransferFunction.from_rational_expression(expr_8, s) == \
|
153 |
+
TransferFunction(2*s**2 + 3*s + 2, s**2 + 1, s)
|
154 |
+
|
155 |
+
# explicitly cancel poles and zeros.
|
156 |
+
tf0 = TransferFunction(s**5 + s**3 + s, s - s**2, s)
|
157 |
+
a = TransferFunction(-(s**4 + s**2 + 1), s - 1, s)
|
158 |
+
assert tf0.simplify() == simplify(tf0) == a
|
159 |
+
|
160 |
+
tf1 = TransferFunction((p + 3)*(p - 1), (p - 1)*(p + 5), p)
|
161 |
+
b = TransferFunction(p + 3, p + 5, p)
|
162 |
+
assert tf1.simplify() == simplify(tf1) == b
|
163 |
+
|
164 |
+
# expand the numerator and the denominator.
|
165 |
+
G1 = TransferFunction((1 - s)**2, (s**2 + 1)**2, s)
|
166 |
+
G2 = TransferFunction(1, -3, p)
|
167 |
+
c = (a2*s**p + a1*s**s + a0*p**p)*(p**s + s**p)
|
168 |
+
d = (b0*s**s + b1*p**s)*(b2*s*p + p**p)
|
169 |
+
e = a0*p**p*p**s + a0*p**p*s**p + a1*p**s*s**s + a1*s**p*s**s + a2*p**s*s**p + a2*s**(2*p)
|
170 |
+
f = b0*b2*p*s*s**s + b0*p**p*s**s + b1*b2*p*p**s*s + b1*p**p*p**s
|
171 |
+
g = a1*a2*s*s**p + a1*p*s + a2*b1*p*s*s**p + b1*p**2*s
|
172 |
+
G3 = TransferFunction(c, d, s)
|
173 |
+
G4 = TransferFunction(a0*s**s - b0*p**p, (a1*s + b1*s*p)*(a2*s**p + p), p)
|
174 |
+
|
175 |
+
assert G1.expand() == TransferFunction(s**2 - 2*s + 1, s**4 + 2*s**2 + 1, s)
|
176 |
+
assert tf1.expand() == TransferFunction(p**2 + 2*p - 3, p**2 + 4*p - 5, p)
|
177 |
+
assert G2.expand() == G2
|
178 |
+
assert G3.expand() == TransferFunction(e, f, s)
|
179 |
+
assert G4.expand() == TransferFunction(a0*s**s - b0*p**p, g, p)
|
180 |
+
|
181 |
+
# purely symbolic polynomials.
|
182 |
+
p1 = a1*s + a0
|
183 |
+
p2 = b2*s**2 + b1*s + b0
|
184 |
+
SP1 = TransferFunction(p1, p2, s)
|
185 |
+
expect1 = TransferFunction(2.0*s + 1.0, 5.0*s**2 + 4.0*s + 3.0, s)
|
186 |
+
expect1_ = TransferFunction(2*s + 1, 5*s**2 + 4*s + 3, s)
|
187 |
+
assert SP1.subs({a0: 1, a1: 2, b0: 3, b1: 4, b2: 5}) == expect1_
|
188 |
+
assert SP1.subs({a0: 1, a1: 2, b0: 3, b1: 4, b2: 5}).evalf() == expect1
|
189 |
+
assert expect1_.evalf() == expect1
|
190 |
+
|
191 |
+
c1, d0, d1, d2 = symbols('c1, d0:3')
|
192 |
+
p3, p4 = c1*p, d2*p**3 + d1*p**2 - d0
|
193 |
+
SP2 = TransferFunction(p3, p4, p)
|
194 |
+
expect2 = TransferFunction(2.0*p, 5.0*p**3 + 2.0*p**2 - 3.0, p)
|
195 |
+
expect2_ = TransferFunction(2*p, 5*p**3 + 2*p**2 - 3, p)
|
196 |
+
assert SP2.subs({c1: 2, d0: 3, d1: 2, d2: 5}) == expect2_
|
197 |
+
assert SP2.subs({c1: 2, d0: 3, d1: 2, d2: 5}).evalf() == expect2
|
198 |
+
assert expect2_.evalf() == expect2
|
199 |
+
|
200 |
+
SP3 = TransferFunction(a0*p**3 + a1*s**2 - b0*s + b1, a1*s + p, s)
|
201 |
+
expect3 = TransferFunction(2.0*p**3 + 4.0*s**2 - s + 5.0, p + 4.0*s, s)
|
202 |
+
expect3_ = TransferFunction(2*p**3 + 4*s**2 - s + 5, p + 4*s, s)
|
203 |
+
assert SP3.subs({a0: 2, a1: 4, b0: 1, b1: 5}) == expect3_
|
204 |
+
assert SP3.subs({a0: 2, a1: 4, b0: 1, b1: 5}).evalf() == expect3
|
205 |
+
assert expect3_.evalf() == expect3
|
206 |
+
|
207 |
+
SP4 = TransferFunction(s - a1*p**3, a0*s + p, p)
|
208 |
+
expect4 = TransferFunction(7.0*p**3 + s, p - s, p)
|
209 |
+
expect4_ = TransferFunction(7*p**3 + s, p - s, p)
|
210 |
+
assert SP4.subs({a0: -1, a1: -7}) == expect4_
|
211 |
+
assert SP4.subs({a0: -1, a1: -7}).evalf() == expect4
|
212 |
+
assert expect4_.evalf() == expect4
|
213 |
+
|
214 |
+
# Low-frequency (or DC) gain.
|
215 |
+
assert tf0.dc_gain() == 1
|
216 |
+
assert tf1.dc_gain() == Rational(3, 5)
|
217 |
+
assert SP2.dc_gain() == 0
|
218 |
+
assert expect4.dc_gain() == -1
|
219 |
+
assert expect2_.dc_gain() == 0
|
220 |
+
assert TransferFunction(1, s, s).dc_gain() == oo
|
221 |
+
|
222 |
+
# Poles of a transfer function.
|
223 |
+
tf_ = TransferFunction(x**3 - k, k, x)
|
224 |
+
_tf = TransferFunction(k, x**4 - k, x)
|
225 |
+
TF_ = TransferFunction(x**2, x**10 + x + x**2, x)
|
226 |
+
_TF = TransferFunction(x**10 + x + x**2, x**2, x)
|
227 |
+
assert G1.poles() == [I, I, -I, -I]
|
228 |
+
assert G2.poles() == []
|
229 |
+
assert tf1.poles() == [-5, 1]
|
230 |
+
assert expect4_.poles() == [s]
|
231 |
+
assert SP4.poles() == [-a0*s]
|
232 |
+
assert expect3.poles() == [-0.25*p]
|
233 |
+
assert str(expect2.poles()) == str([0.729001428685125, -0.564500714342563 - 0.710198984796332*I, -0.564500714342563 + 0.710198984796332*I])
|
234 |
+
assert str(expect1.poles()) == str([-0.4 - 0.66332495807108*I, -0.4 + 0.66332495807108*I])
|
235 |
+
assert _tf.poles() == [k**(Rational(1, 4)), -k**(Rational(1, 4)), I*k**(Rational(1, 4)), -I*k**(Rational(1, 4))]
|
236 |
+
assert TF_.poles() == [CRootOf(x**9 + x + 1, 0), 0, CRootOf(x**9 + x + 1, 1), CRootOf(x**9 + x + 1, 2),
|
237 |
+
CRootOf(x**9 + x + 1, 3), CRootOf(x**9 + x + 1, 4), CRootOf(x**9 + x + 1, 5), CRootOf(x**9 + x + 1, 6),
|
238 |
+
CRootOf(x**9 + x + 1, 7), CRootOf(x**9 + x + 1, 8)]
|
239 |
+
raises(NotImplementedError, lambda: TransferFunction(x**2, a0*x**10 + x + x**2, x).poles())
|
240 |
+
|
241 |
+
# Stability of a transfer function.
|
242 |
+
q, r = symbols('q, r', negative=True)
|
243 |
+
t = symbols('t', positive=True)
|
244 |
+
TF_ = TransferFunction(s**2 + a0 - a1*p, q*s - r, s)
|
245 |
+
stable_tf = TransferFunction(s**2 + a0 - a1*p, q*s - 1, s)
|
246 |
+
stable_tf_ = TransferFunction(s**2 + a0 - a1*p, q*s - t, s)
|
247 |
+
|
248 |
+
assert G1.is_stable() is False
|
249 |
+
assert G2.is_stable() is True
|
250 |
+
assert tf1.is_stable() is False # as one pole is +ve, and the other is -ve.
|
251 |
+
assert expect2.is_stable() is False
|
252 |
+
assert expect1.is_stable() is True
|
253 |
+
assert stable_tf.is_stable() is True
|
254 |
+
assert stable_tf_.is_stable() is True
|
255 |
+
assert TF_.is_stable() is False
|
256 |
+
assert expect4_.is_stable() is None # no assumption provided for the only pole 's'.
|
257 |
+
assert SP4.is_stable() is None
|
258 |
+
|
259 |
+
# Zeros of a transfer function.
|
260 |
+
assert G1.zeros() == [1, 1]
|
261 |
+
assert G2.zeros() == []
|
262 |
+
assert tf1.zeros() == [-3, 1]
|
263 |
+
assert expect4_.zeros() == [7**(Rational(2, 3))*(-s)**(Rational(1, 3))/7, -7**(Rational(2, 3))*(-s)**(Rational(1, 3))/14 -
|
264 |
+
sqrt(3)*7**(Rational(2, 3))*I*(-s)**(Rational(1, 3))/14, -7**(Rational(2, 3))*(-s)**(Rational(1, 3))/14 + sqrt(3)*7**(Rational(2, 3))*I*(-s)**(Rational(1, 3))/14]
|
265 |
+
assert SP4.zeros() == [(s/a1)**(Rational(1, 3)), -(s/a1)**(Rational(1, 3))/2 - sqrt(3)*I*(s/a1)**(Rational(1, 3))/2,
|
266 |
+
-(s/a1)**(Rational(1, 3))/2 + sqrt(3)*I*(s/a1)**(Rational(1, 3))/2]
|
267 |
+
assert str(expect3.zeros()) == str([0.125 - 1.11102430216445*sqrt(-0.405063291139241*p**3 - 1.0),
|
268 |
+
1.11102430216445*sqrt(-0.405063291139241*p**3 - 1.0) + 0.125])
|
269 |
+
assert tf_.zeros() == [k**(Rational(1, 3)), -k**(Rational(1, 3))/2 - sqrt(3)*I*k**(Rational(1, 3))/2,
|
270 |
+
-k**(Rational(1, 3))/2 + sqrt(3)*I*k**(Rational(1, 3))/2]
|
271 |
+
assert _TF.zeros() == [CRootOf(x**9 + x + 1, 0), 0, CRootOf(x**9 + x + 1, 1), CRootOf(x**9 + x + 1, 2),
|
272 |
+
CRootOf(x**9 + x + 1, 3), CRootOf(x**9 + x + 1, 4), CRootOf(x**9 + x + 1, 5), CRootOf(x**9 + x + 1, 6),
|
273 |
+
CRootOf(x**9 + x + 1, 7), CRootOf(x**9 + x + 1, 8)]
|
274 |
+
raises(NotImplementedError, lambda: TransferFunction(a0*x**10 + x + x**2, x**2, x).zeros())
|
275 |
+
|
276 |
+
# negation of TF.
|
277 |
+
tf2 = TransferFunction(s + 3, s**2 - s**3 + 9, s)
|
278 |
+
tf3 = TransferFunction(-3*p + 3, 1 - p, p)
|
279 |
+
assert -tf2 == TransferFunction(-s - 3, s**2 - s**3 + 9, s)
|
280 |
+
assert -tf3 == TransferFunction(3*p - 3, 1 - p, p)
|
281 |
+
|
282 |
+
# taking power of a TF.
|
283 |
+
tf4 = TransferFunction(p + 4, p - 3, p)
|
284 |
+
tf5 = TransferFunction(s**2 + 1, 1 - s, s)
|
285 |
+
expect2 = TransferFunction((s**2 + 1)**3, (1 - s)**3, s)
|
286 |
+
expect1 = TransferFunction((p + 4)**2, (p - 3)**2, p)
|
287 |
+
assert (tf4*tf4).doit() == tf4**2 == pow(tf4, 2) == expect1
|
288 |
+
assert (tf5*tf5*tf5).doit() == tf5**3 == pow(tf5, 3) == expect2
|
289 |
+
assert tf5**0 == pow(tf5, 0) == TransferFunction(1, 1, s)
|
290 |
+
assert Series(tf4).doit()**-1 == tf4**-1 == pow(tf4, -1) == TransferFunction(p - 3, p + 4, p)
|
291 |
+
assert (tf5*tf5).doit()**-1 == tf5**-2 == pow(tf5, -2) == TransferFunction((1 - s)**2, (s**2 + 1)**2, s)
|
292 |
+
|
293 |
+
raises(ValueError, lambda: tf4**(s**2 + s - 1))
|
294 |
+
raises(ValueError, lambda: tf5**s)
|
295 |
+
raises(ValueError, lambda: tf4**tf5)
|
296 |
+
|
297 |
+
# SymPy's own functions.
|
298 |
+
tf = TransferFunction(s - 1, s**2 - 2*s + 1, s)
|
299 |
+
tf6 = TransferFunction(s + p, p**2 - 5, s)
|
300 |
+
assert factor(tf) == TransferFunction(s - 1, (s - 1)**2, s)
|
301 |
+
assert tf.num.subs(s, 2) == tf.den.subs(s, 2) == 1
|
302 |
+
# subs & xreplace
|
303 |
+
assert tf.subs(s, 2) == TransferFunction(s - 1, s**2 - 2*s + 1, s)
|
304 |
+
assert tf6.subs(p, 3) == TransferFunction(s + 3, 4, s)
|
305 |
+
assert tf3.xreplace({p: s}) == TransferFunction(-3*s + 3, 1 - s, s)
|
306 |
+
raises(TypeError, lambda: tf3.xreplace({p: exp(2)}))
|
307 |
+
assert tf3.subs(p, exp(2)) == tf3
|
308 |
+
|
309 |
+
tf7 = TransferFunction(a0*s**p + a1*p**s, a2*p - s, s)
|
310 |
+
assert tf7.xreplace({s: k}) == TransferFunction(a0*k**p + a1*p**k, a2*p - k, k)
|
311 |
+
assert tf7.subs(s, k) == TransferFunction(a0*s**p + a1*p**s, a2*p - s, s)
|
312 |
+
|
313 |
+
# Conversion to Expr with to_expr()
|
314 |
+
tf8 = TransferFunction(a0*s**5 + 5*s**2 + 3, s**6 - 3, s)
|
315 |
+
tf9 = TransferFunction((5 + s), (5 + s)*(6 + s), s)
|
316 |
+
tf10 = TransferFunction(0, 1, s)
|
317 |
+
tf11 = TransferFunction(1, 1, s)
|
318 |
+
assert tf8.to_expr() == Mul((a0*s**5 + 5*s**2 + 3), Pow((s**6 - 3), -1, evaluate=False), evaluate=False)
|
319 |
+
assert tf9.to_expr() == Mul((s + 5), Pow((5 + s)*(6 + s), -1, evaluate=False), evaluate=False)
|
320 |
+
assert tf10.to_expr() == Mul(S(0), Pow(1, -1, evaluate=False), evaluate=False)
|
321 |
+
assert tf11.to_expr() == Pow(1, -1, evaluate=False)
|
322 |
+
|
323 |
+
def test_TransferFunction_addition_and_subtraction():
|
324 |
+
tf1 = TransferFunction(s + 6, s - 5, s)
|
325 |
+
tf2 = TransferFunction(s + 3, s + 1, s)
|
326 |
+
tf3 = TransferFunction(s + 1, s**2 + s + 1, s)
|
327 |
+
tf4 = TransferFunction(p, 2 - p, p)
|
328 |
+
|
329 |
+
# addition
|
330 |
+
assert tf1 + tf2 == Parallel(tf1, tf2)
|
331 |
+
assert tf3 + tf1 == Parallel(tf3, tf1)
|
332 |
+
assert -tf1 + tf2 + tf3 == Parallel(-tf1, tf2, tf3)
|
333 |
+
assert tf1 + (tf2 + tf3) == Parallel(tf1, tf2, tf3)
|
334 |
+
|
335 |
+
c = symbols("c", commutative=False)
|
336 |
+
raises(ValueError, lambda: tf1 + Matrix([1, 2, 3]))
|
337 |
+
raises(ValueError, lambda: tf2 + c)
|
338 |
+
raises(ValueError, lambda: tf3 + tf4)
|
339 |
+
raises(ValueError, lambda: tf1 + (s - 1))
|
340 |
+
raises(ValueError, lambda: tf1 + 8)
|
341 |
+
raises(ValueError, lambda: (1 - p**3) + tf1)
|
342 |
+
|
343 |
+
# subtraction
|
344 |
+
assert tf1 - tf2 == Parallel(tf1, -tf2)
|
345 |
+
assert tf3 - tf2 == Parallel(tf3, -tf2)
|
346 |
+
assert -tf1 - tf3 == Parallel(-tf1, -tf3)
|
347 |
+
assert tf1 - tf2 + tf3 == Parallel(tf1, -tf2, tf3)
|
348 |
+
|
349 |
+
raises(ValueError, lambda: tf1 - Matrix([1, 2, 3]))
|
350 |
+
raises(ValueError, lambda: tf3 - tf4)
|
351 |
+
raises(ValueError, lambda: tf1 - (s - 1))
|
352 |
+
raises(ValueError, lambda: tf1 - 8)
|
353 |
+
raises(ValueError, lambda: (s + 5) - tf2)
|
354 |
+
raises(ValueError, lambda: (1 + p**4) - tf1)
|
355 |
+
|
356 |
+
|
357 |
+
def test_TransferFunction_multiplication_and_division():
|
358 |
+
G1 = TransferFunction(s + 3, -s**3 + 9, s)
|
359 |
+
G2 = TransferFunction(s + 1, s - 5, s)
|
360 |
+
G3 = TransferFunction(p, p**4 - 6, p)
|
361 |
+
G4 = TransferFunction(p + 4, p - 5, p)
|
362 |
+
G5 = TransferFunction(s + 6, s - 5, s)
|
363 |
+
G6 = TransferFunction(s + 3, s + 1, s)
|
364 |
+
G7 = TransferFunction(1, 1, s)
|
365 |
+
|
366 |
+
# multiplication
|
367 |
+
assert G1*G2 == Series(G1, G2)
|
368 |
+
assert -G1*G5 == Series(-G1, G5)
|
369 |
+
assert -G2*G5*-G6 == Series(-G2, G5, -G6)
|
370 |
+
assert -G1*-G2*-G5*-G6 == Series(-G1, -G2, -G5, -G6)
|
371 |
+
assert G3*G4 == Series(G3, G4)
|
372 |
+
assert (G1*G2)*-(G5*G6) == \
|
373 |
+
Series(G1, G2, TransferFunction(-1, 1, s), Series(G5, G6))
|
374 |
+
assert G1*G2*(G5 + G6) == Series(G1, G2, Parallel(G5, G6))
|
375 |
+
|
376 |
+
c = symbols("c", commutative=False)
|
377 |
+
raises(ValueError, lambda: G3 * Matrix([1, 2, 3]))
|
378 |
+
raises(ValueError, lambda: G1 * c)
|
379 |
+
raises(ValueError, lambda: G3 * G5)
|
380 |
+
raises(ValueError, lambda: G5 * (s - 1))
|
381 |
+
raises(ValueError, lambda: 9 * G5)
|
382 |
+
|
383 |
+
raises(ValueError, lambda: G3 / Matrix([1, 2, 3]))
|
384 |
+
raises(ValueError, lambda: G6 / 0)
|
385 |
+
raises(ValueError, lambda: G3 / G5)
|
386 |
+
raises(ValueError, lambda: G5 / 2)
|
387 |
+
raises(ValueError, lambda: G5 / s**2)
|
388 |
+
raises(ValueError, lambda: (s - 4*s**2) / G2)
|
389 |
+
raises(ValueError, lambda: 0 / G4)
|
390 |
+
raises(ValueError, lambda: G5 / G6)
|
391 |
+
raises(ValueError, lambda: -G3 /G4)
|
392 |
+
raises(ValueError, lambda: G7 / (1 + G6))
|
393 |
+
raises(ValueError, lambda: G7 / (G5 * G6))
|
394 |
+
raises(ValueError, lambda: G7 / (G7 + (G5 + G6)))
|
395 |
+
|
396 |
+
|
397 |
+
def test_TransferFunction_is_proper():
|
398 |
+
omega_o, zeta, tau = symbols('omega_o, zeta, tau')
|
399 |
+
G1 = TransferFunction(omega_o**2, s**2 + p*omega_o*zeta*s + omega_o**2, omega_o)
|
400 |
+
G2 = TransferFunction(tau - s**3, tau + p**4, tau)
|
401 |
+
G3 = TransferFunction(a*b*s**3 + s**2 - a*p + s, b - s*p**2, p)
|
402 |
+
G4 = TransferFunction(b*s**2 + p**2 - a*p + s, b - p**2, s)
|
403 |
+
assert G1.is_proper
|
404 |
+
assert G2.is_proper
|
405 |
+
assert G3.is_proper
|
406 |
+
assert not G4.is_proper
|
407 |
+
|
408 |
+
|
409 |
+
def test_TransferFunction_is_strictly_proper():
|
410 |
+
omega_o, zeta, tau = symbols('omega_o, zeta, tau')
|
411 |
+
tf1 = TransferFunction(omega_o**2, s**2 + p*omega_o*zeta*s + omega_o**2, omega_o)
|
412 |
+
tf2 = TransferFunction(tau - s**3, tau + p**4, tau)
|
413 |
+
tf3 = TransferFunction(a*b*s**3 + s**2 - a*p + s, b - s*p**2, p)
|
414 |
+
tf4 = TransferFunction(b*s**2 + p**2 - a*p + s, b - p**2, s)
|
415 |
+
assert not tf1.is_strictly_proper
|
416 |
+
assert not tf2.is_strictly_proper
|
417 |
+
assert tf3.is_strictly_proper
|
418 |
+
assert not tf4.is_strictly_proper
|
419 |
+
|
420 |
+
|
421 |
+
def test_TransferFunction_is_biproper():
|
422 |
+
tau, omega_o, zeta = symbols('tau, omega_o, zeta')
|
423 |
+
tf1 = TransferFunction(omega_o**2, s**2 + p*omega_o*zeta*s + omega_o**2, omega_o)
|
424 |
+
tf2 = TransferFunction(tau - s**3, tau + p**4, tau)
|
425 |
+
tf3 = TransferFunction(a*b*s**3 + s**2 - a*p + s, b - s*p**2, p)
|
426 |
+
tf4 = TransferFunction(b*s**2 + p**2 - a*p + s, b - p**2, s)
|
427 |
+
assert tf1.is_biproper
|
428 |
+
assert tf2.is_biproper
|
429 |
+
assert not tf3.is_biproper
|
430 |
+
assert not tf4.is_biproper
|
431 |
+
|
432 |
+
|
433 |
+
def test_Series_construction():
|
434 |
+
tf = TransferFunction(a0*s**3 + a1*s**2 - a2*s, b0*p**4 + b1*p**3 - b2*s*p, s)
|
435 |
+
tf2 = TransferFunction(a2*p - s, a2*s + p, s)
|
436 |
+
tf3 = TransferFunction(a0*p + p**a1 - s, p, p)
|
437 |
+
tf4 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
438 |
+
inp = Function('X_d')(s)
|
439 |
+
out = Function('X')(s)
|
440 |
+
|
441 |
+
s0 = Series(tf, tf2)
|
442 |
+
assert s0.args == (tf, tf2)
|
443 |
+
assert s0.var == s
|
444 |
+
|
445 |
+
s1 = Series(Parallel(tf, -tf2), tf2)
|
446 |
+
assert s1.args == (Parallel(tf, -tf2), tf2)
|
447 |
+
assert s1.var == s
|
448 |
+
|
449 |
+
tf3_ = TransferFunction(inp, 1, s)
|
450 |
+
tf4_ = TransferFunction(-out, 1, s)
|
451 |
+
s2 = Series(tf, Parallel(tf3_, tf4_), tf2)
|
452 |
+
assert s2.args == (tf, Parallel(tf3_, tf4_), tf2)
|
453 |
+
|
454 |
+
s3 = Series(tf, tf2, tf4)
|
455 |
+
assert s3.args == (tf, tf2, tf4)
|
456 |
+
|
457 |
+
s4 = Series(tf3_, tf4_)
|
458 |
+
assert s4.args == (tf3_, tf4_)
|
459 |
+
assert s4.var == s
|
460 |
+
|
461 |
+
s6 = Series(tf2, tf4, Parallel(tf2, -tf), tf4)
|
462 |
+
assert s6.args == (tf2, tf4, Parallel(tf2, -tf), tf4)
|
463 |
+
|
464 |
+
s7 = Series(tf, tf2)
|
465 |
+
assert s0 == s7
|
466 |
+
assert not s0 == s2
|
467 |
+
|
468 |
+
raises(ValueError, lambda: Series(tf, tf3))
|
469 |
+
raises(ValueError, lambda: Series(tf, tf2, tf3, tf4))
|
470 |
+
raises(ValueError, lambda: Series(-tf3, tf2))
|
471 |
+
raises(TypeError, lambda: Series(2, tf, tf4))
|
472 |
+
raises(TypeError, lambda: Series(s**2 + p*s, tf3, tf2))
|
473 |
+
raises(TypeError, lambda: Series(tf3, Matrix([1, 2, 3, 4])))
|
474 |
+
|
475 |
+
|
476 |
+
def test_MIMOSeries_construction():
|
477 |
+
tf_1 = TransferFunction(a0*s**3 + a1*s**2 - a2*s, b0*p**4 + b1*p**3 - b2*s*p, s)
|
478 |
+
tf_2 = TransferFunction(a2*p - s, a2*s + p, s)
|
479 |
+
tf_3 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
480 |
+
|
481 |
+
tfm_1 = TransferFunctionMatrix([[tf_1, tf_2, tf_3], [-tf_3, -tf_2, tf_1]])
|
482 |
+
tfm_2 = TransferFunctionMatrix([[-tf_2], [-tf_2], [-tf_3]])
|
483 |
+
tfm_3 = TransferFunctionMatrix([[-tf_3]])
|
484 |
+
tfm_4 = TransferFunctionMatrix([[TF3], [TF2], [-TF1]])
|
485 |
+
tfm_5 = TransferFunctionMatrix.from_Matrix(Matrix([1/p]), p)
|
486 |
+
|
487 |
+
s8 = MIMOSeries(tfm_2, tfm_1)
|
488 |
+
assert s8.args == (tfm_2, tfm_1)
|
489 |
+
assert s8.var == s
|
490 |
+
assert s8.shape == (s8.num_outputs, s8.num_inputs) == (2, 1)
|
491 |
+
|
492 |
+
s9 = MIMOSeries(tfm_3, tfm_2, tfm_1)
|
493 |
+
assert s9.args == (tfm_3, tfm_2, tfm_1)
|
494 |
+
assert s9.var == s
|
495 |
+
assert s9.shape == (s9.num_outputs, s9.num_inputs) == (2, 1)
|
496 |
+
|
497 |
+
s11 = MIMOSeries(tfm_3, MIMOParallel(-tfm_2, -tfm_4), tfm_1)
|
498 |
+
assert s11.args == (tfm_3, MIMOParallel(-tfm_2, -tfm_4), tfm_1)
|
499 |
+
assert s11.shape == (s11.num_outputs, s11.num_inputs) == (2, 1)
|
500 |
+
|
501 |
+
# arg cannot be empty tuple.
|
502 |
+
raises(ValueError, lambda: MIMOSeries())
|
503 |
+
|
504 |
+
# arg cannot contain SISO as well as MIMO systems.
|
505 |
+
raises(TypeError, lambda: MIMOSeries(tfm_1, tf_1))
|
506 |
+
|
507 |
+
# for all the adjacent transfer function matrices:
|
508 |
+
# no. of inputs of first TFM must be equal to the no. of outputs of the second TFM.
|
509 |
+
raises(ValueError, lambda: MIMOSeries(tfm_1, tfm_2, -tfm_1))
|
510 |
+
|
511 |
+
# all the TFMs must use the same complex variable.
|
512 |
+
raises(ValueError, lambda: MIMOSeries(tfm_3, tfm_5))
|
513 |
+
|
514 |
+
# Number or expression not allowed in the arguments.
|
515 |
+
raises(TypeError, lambda: MIMOSeries(2, tfm_2, tfm_3))
|
516 |
+
raises(TypeError, lambda: MIMOSeries(s**2 + p*s, -tfm_2, tfm_3))
|
517 |
+
raises(TypeError, lambda: MIMOSeries(Matrix([1/p]), tfm_3))
|
518 |
+
|
519 |
+
|
520 |
+
def test_Series_functions():
|
521 |
+
tf1 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
522 |
+
tf2 = TransferFunction(k, 1, s)
|
523 |
+
tf3 = TransferFunction(a2*p - s, a2*s + p, s)
|
524 |
+
tf4 = TransferFunction(a0*p + p**a1 - s, p, p)
|
525 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
526 |
+
|
527 |
+
assert tf1*tf2*tf3 == Series(tf1, tf2, tf3) == Series(Series(tf1, tf2), tf3) \
|
528 |
+
== Series(tf1, Series(tf2, tf3))
|
529 |
+
assert tf1*(tf2 + tf3) == Series(tf1, Parallel(tf2, tf3))
|
530 |
+
assert tf1*tf2 + tf5 == Parallel(Series(tf1, tf2), tf5)
|
531 |
+
assert tf1*tf2 - tf5 == Parallel(Series(tf1, tf2), -tf5)
|
532 |
+
assert tf1*tf2 + tf3 + tf5 == Parallel(Series(tf1, tf2), tf3, tf5)
|
533 |
+
assert tf1*tf2 - tf3 - tf5 == Parallel(Series(tf1, tf2), -tf3, -tf5)
|
534 |
+
assert tf1*tf2 - tf3 + tf5 == Parallel(Series(tf1, tf2), -tf3, tf5)
|
535 |
+
assert tf1*tf2 + tf3*tf5 == Parallel(Series(tf1, tf2), Series(tf3, tf5))
|
536 |
+
assert tf1*tf2 - tf3*tf5 == Parallel(Series(tf1, tf2), Series(TransferFunction(-1, 1, s), Series(tf3, tf5)))
|
537 |
+
assert tf2*tf3*(tf2 - tf1)*tf3 == Series(tf2, tf3, Parallel(tf2, -tf1), tf3)
|
538 |
+
assert -tf1*tf2 == Series(-tf1, tf2)
|
539 |
+
assert -(tf1*tf2) == Series(TransferFunction(-1, 1, s), Series(tf1, tf2))
|
540 |
+
raises(ValueError, lambda: tf1*tf2*tf4)
|
541 |
+
raises(ValueError, lambda: tf1*(tf2 - tf4))
|
542 |
+
raises(ValueError, lambda: tf3*Matrix([1, 2, 3]))
|
543 |
+
|
544 |
+
# evaluate=True -> doit()
|
545 |
+
assert Series(tf1, tf2, evaluate=True) == Series(tf1, tf2).doit() == \
|
546 |
+
TransferFunction(k, s**2 + 2*s*wn*zeta + wn**2, s)
|
547 |
+
assert Series(tf1, tf2, Parallel(tf1, -tf3), evaluate=True) == Series(tf1, tf2, Parallel(tf1, -tf3)).doit() == \
|
548 |
+
TransferFunction(k*(a2*s + p + (-a2*p + s)*(s**2 + 2*s*wn*zeta + wn**2)), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2)**2, s)
|
549 |
+
assert Series(tf2, tf1, -tf3, evaluate=True) == Series(tf2, tf1, -tf3).doit() == \
|
550 |
+
TransferFunction(k*(-a2*p + s), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
551 |
+
assert not Series(tf1, -tf2, evaluate=False) == Series(tf1, -tf2).doit()
|
552 |
+
|
553 |
+
assert Series(Parallel(tf1, tf2), Parallel(tf2, -tf3)).doit() == \
|
554 |
+
TransferFunction((k*(s**2 + 2*s*wn*zeta + wn**2) + 1)*(-a2*p + k*(a2*s + p) + s), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
555 |
+
assert Series(-tf1, -tf2, -tf3).doit() == \
|
556 |
+
TransferFunction(k*(-a2*p + s), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
557 |
+
assert -Series(tf1, tf2, tf3).doit() == \
|
558 |
+
TransferFunction(-k*(a2*p - s), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
559 |
+
assert Series(tf2, tf3, Parallel(tf2, -tf1), tf3).doit() == \
|
560 |
+
TransferFunction(k*(a2*p - s)**2*(k*(s**2 + 2*s*wn*zeta + wn**2) - 1), (a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2), s)
|
561 |
+
|
562 |
+
assert Series(tf1, tf2).rewrite(TransferFunction) == TransferFunction(k, s**2 + 2*s*wn*zeta + wn**2, s)
|
563 |
+
assert Series(tf2, tf1, -tf3).rewrite(TransferFunction) == \
|
564 |
+
TransferFunction(k*(-a2*p + s), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
565 |
+
|
566 |
+
S1 = Series(Parallel(tf1, tf2), Parallel(tf2, -tf3))
|
567 |
+
assert S1.is_proper
|
568 |
+
assert not S1.is_strictly_proper
|
569 |
+
assert S1.is_biproper
|
570 |
+
|
571 |
+
S2 = Series(tf1, tf2, tf3)
|
572 |
+
assert S2.is_proper
|
573 |
+
assert S2.is_strictly_proper
|
574 |
+
assert not S2.is_biproper
|
575 |
+
|
576 |
+
S3 = Series(tf1, -tf2, Parallel(tf1, -tf3))
|
577 |
+
assert S3.is_proper
|
578 |
+
assert S3.is_strictly_proper
|
579 |
+
assert not S3.is_biproper
|
580 |
+
|
581 |
+
|
582 |
+
def test_MIMOSeries_functions():
|
583 |
+
tfm1 = TransferFunctionMatrix([[TF1, TF2, TF3], [-TF3, -TF2, TF1]])
|
584 |
+
tfm2 = TransferFunctionMatrix([[-TF1], [-TF2], [-TF3]])
|
585 |
+
tfm3 = TransferFunctionMatrix([[-TF1]])
|
586 |
+
tfm4 = TransferFunctionMatrix([[-TF2, -TF3], [-TF1, TF2]])
|
587 |
+
tfm5 = TransferFunctionMatrix([[TF2, -TF2], [-TF3, -TF2]])
|
588 |
+
tfm6 = TransferFunctionMatrix([[-TF3], [TF1]])
|
589 |
+
tfm7 = TransferFunctionMatrix([[TF1], [-TF2]])
|
590 |
+
|
591 |
+
assert tfm1*tfm2 + tfm6 == MIMOParallel(MIMOSeries(tfm2, tfm1), tfm6)
|
592 |
+
assert tfm1*tfm2 + tfm7 + tfm6 == MIMOParallel(MIMOSeries(tfm2, tfm1), tfm7, tfm6)
|
593 |
+
assert tfm1*tfm2 - tfm6 - tfm7 == MIMOParallel(MIMOSeries(tfm2, tfm1), -tfm6, -tfm7)
|
594 |
+
assert tfm4*tfm5 + (tfm4 - tfm5) == MIMOParallel(MIMOSeries(tfm5, tfm4), tfm4, -tfm5)
|
595 |
+
assert tfm4*-tfm6 + (-tfm4*tfm6) == MIMOParallel(MIMOSeries(-tfm6, tfm4), MIMOSeries(tfm6, -tfm4))
|
596 |
+
|
597 |
+
raises(ValueError, lambda: tfm1*tfm2 + TF1)
|
598 |
+
raises(TypeError, lambda: tfm1*tfm2 + a0)
|
599 |
+
raises(TypeError, lambda: tfm4*tfm6 - (s - 1))
|
600 |
+
raises(TypeError, lambda: tfm4*-tfm6 - 8)
|
601 |
+
raises(TypeError, lambda: (-1 + p**5) + tfm1*tfm2)
|
602 |
+
|
603 |
+
# Shape criteria.
|
604 |
+
|
605 |
+
raises(TypeError, lambda: -tfm1*tfm2 + tfm4)
|
606 |
+
raises(TypeError, lambda: tfm1*tfm2 - tfm4 + tfm5)
|
607 |
+
raises(TypeError, lambda: tfm1*tfm2 - tfm4*tfm5)
|
608 |
+
|
609 |
+
assert tfm1*tfm2*-tfm3 == MIMOSeries(-tfm3, tfm2, tfm1)
|
610 |
+
assert (tfm1*-tfm2)*tfm3 == MIMOSeries(tfm3, -tfm2, tfm1)
|
611 |
+
|
612 |
+
# Multiplication of a Series object with a SISO TF not allowed.
|
613 |
+
|
614 |
+
raises(ValueError, lambda: tfm4*tfm5*TF1)
|
615 |
+
raises(TypeError, lambda: tfm4*tfm5*a1)
|
616 |
+
raises(TypeError, lambda: tfm4*-tfm5*(s - 2))
|
617 |
+
raises(TypeError, lambda: tfm5*tfm4*9)
|
618 |
+
raises(TypeError, lambda: (-p**3 + 1)*tfm5*tfm4)
|
619 |
+
|
620 |
+
# Transfer function matrix in the arguments.
|
621 |
+
assert (MIMOSeries(tfm2, tfm1, evaluate=True) == MIMOSeries(tfm2, tfm1).doit()
|
622 |
+
== TransferFunctionMatrix(((TransferFunction(-k**2*(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**2 + (-a2*p + s)*(a2*p - s)*(s**2 + 2*s*wn*zeta + wn**2)**2 - (a2*s + p)**2,
|
623 |
+
(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**2, s),),
|
624 |
+
(TransferFunction(k**2*(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**2 + (-a2*p + s)*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) + (a2*p - s)*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2),
|
625 |
+
(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**2, s),))))
|
626 |
+
|
627 |
+
# doit() should not cancel poles and zeros.
|
628 |
+
mat_1 = Matrix([[1/(1+s), (1+s)/(1+s**2+2*s)**3]])
|
629 |
+
mat_2 = Matrix([[(1+s)], [(1+s**2+2*s)**3/(1+s)]])
|
630 |
+
tm_1, tm_2 = TransferFunctionMatrix.from_Matrix(mat_1, s), TransferFunctionMatrix.from_Matrix(mat_2, s)
|
631 |
+
assert (MIMOSeries(tm_2, tm_1).doit()
|
632 |
+
== TransferFunctionMatrix(((TransferFunction(2*(s + 1)**2*(s**2 + 2*s + 1)**3, (s + 1)**2*(s**2 + 2*s + 1)**3, s),),)))
|
633 |
+
assert MIMOSeries(tm_2, tm_1).doit().simplify() == TransferFunctionMatrix(((TransferFunction(2, 1, s),),))
|
634 |
+
|
635 |
+
# calling doit() will expand the internal Series and Parallel objects.
|
636 |
+
assert (MIMOSeries(-tfm3, -tfm2, tfm1, evaluate=True)
|
637 |
+
== MIMOSeries(-tfm3, -tfm2, tfm1).doit()
|
638 |
+
== TransferFunctionMatrix(((TransferFunction(k**2*(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**2 + (a2*p - s)**2*(s**2 + 2*s*wn*zeta + wn**2)**2 + (a2*s + p)**2,
|
639 |
+
(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**3, s),),
|
640 |
+
(TransferFunction(-k**2*(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**2 + (-a2*p + s)*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) + (a2*p - s)*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2),
|
641 |
+
(a2*s + p)**2*(s**2 + 2*s*wn*zeta + wn**2)**3, s),))))
|
642 |
+
assert (MIMOSeries(MIMOParallel(tfm4, tfm5), tfm5, evaluate=True)
|
643 |
+
== MIMOSeries(MIMOParallel(tfm4, tfm5), tfm5).doit()
|
644 |
+
== TransferFunctionMatrix(((TransferFunction(-k*(-a2*s - p + (-a2*p + s)*(s**2 + 2*s*wn*zeta + wn**2)), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s), TransferFunction(k*(-a2*p - \
|
645 |
+
k*(a2*s + p) + s), a2*s + p, s)), (TransferFunction(-k*(-a2*s - p + (-a2*p + s)*(s**2 + 2*s*wn*zeta + wn**2)), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s), \
|
646 |
+
TransferFunction((-a2*p + s)*(-a2*p - k*(a2*s + p) + s), (a2*s + p)**2, s)))) == MIMOSeries(MIMOParallel(tfm4, tfm5), tfm5).rewrite(TransferFunctionMatrix))
|
647 |
+
|
648 |
+
|
649 |
+
def test_Parallel_construction():
|
650 |
+
tf = TransferFunction(a0*s**3 + a1*s**2 - a2*s, b0*p**4 + b1*p**3 - b2*s*p, s)
|
651 |
+
tf2 = TransferFunction(a2*p - s, a2*s + p, s)
|
652 |
+
tf3 = TransferFunction(a0*p + p**a1 - s, p, p)
|
653 |
+
tf4 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
654 |
+
inp = Function('X_d')(s)
|
655 |
+
out = Function('X')(s)
|
656 |
+
|
657 |
+
p0 = Parallel(tf, tf2)
|
658 |
+
assert p0.args == (tf, tf2)
|
659 |
+
assert p0.var == s
|
660 |
+
|
661 |
+
p1 = Parallel(Series(tf, -tf2), tf2)
|
662 |
+
assert p1.args == (Series(tf, -tf2), tf2)
|
663 |
+
assert p1.var == s
|
664 |
+
|
665 |
+
tf3_ = TransferFunction(inp, 1, s)
|
666 |
+
tf4_ = TransferFunction(-out, 1, s)
|
667 |
+
p2 = Parallel(tf, Series(tf3_, -tf4_), tf2)
|
668 |
+
assert p2.args == (tf, Series(tf3_, -tf4_), tf2)
|
669 |
+
|
670 |
+
p3 = Parallel(tf, tf2, tf4)
|
671 |
+
assert p3.args == (tf, tf2, tf4)
|
672 |
+
|
673 |
+
p4 = Parallel(tf3_, tf4_)
|
674 |
+
assert p4.args == (tf3_, tf4_)
|
675 |
+
assert p4.var == s
|
676 |
+
|
677 |
+
p5 = Parallel(tf, tf2)
|
678 |
+
assert p0 == p5
|
679 |
+
assert not p0 == p1
|
680 |
+
|
681 |
+
p6 = Parallel(tf2, tf4, Series(tf2, -tf4))
|
682 |
+
assert p6.args == (tf2, tf4, Series(tf2, -tf4))
|
683 |
+
|
684 |
+
p7 = Parallel(tf2, tf4, Series(tf2, -tf), tf4)
|
685 |
+
assert p7.args == (tf2, tf4, Series(tf2, -tf), tf4)
|
686 |
+
|
687 |
+
raises(ValueError, lambda: Parallel(tf, tf3))
|
688 |
+
raises(ValueError, lambda: Parallel(tf, tf2, tf3, tf4))
|
689 |
+
raises(ValueError, lambda: Parallel(-tf3, tf4))
|
690 |
+
raises(TypeError, lambda: Parallel(2, tf, tf4))
|
691 |
+
raises(TypeError, lambda: Parallel(s**2 + p*s, tf3, tf2))
|
692 |
+
raises(TypeError, lambda: Parallel(tf3, Matrix([1, 2, 3, 4])))
|
693 |
+
|
694 |
+
|
695 |
+
def test_MIMOParallel_construction():
|
696 |
+
tfm1 = TransferFunctionMatrix([[TF1], [TF2], [TF3]])
|
697 |
+
tfm2 = TransferFunctionMatrix([[-TF3], [TF2], [TF1]])
|
698 |
+
tfm3 = TransferFunctionMatrix([[TF1]])
|
699 |
+
tfm4 = TransferFunctionMatrix([[TF2], [TF1], [TF3]])
|
700 |
+
tfm5 = TransferFunctionMatrix([[TF1, TF2], [TF2, TF1]])
|
701 |
+
tfm6 = TransferFunctionMatrix([[TF2, TF1], [TF1, TF2]])
|
702 |
+
tfm7 = TransferFunctionMatrix.from_Matrix(Matrix([[1/p]]), p)
|
703 |
+
|
704 |
+
p8 = MIMOParallel(tfm1, tfm2)
|
705 |
+
assert p8.args == (tfm1, tfm2)
|
706 |
+
assert p8.var == s
|
707 |
+
assert p8.shape == (p8.num_outputs, p8.num_inputs) == (3, 1)
|
708 |
+
|
709 |
+
p9 = MIMOParallel(MIMOSeries(tfm3, tfm1), tfm2)
|
710 |
+
assert p9.args == (MIMOSeries(tfm3, tfm1), tfm2)
|
711 |
+
assert p9.var == s
|
712 |
+
assert p9.shape == (p9.num_outputs, p9.num_inputs) == (3, 1)
|
713 |
+
|
714 |
+
p10 = MIMOParallel(tfm1, MIMOSeries(tfm3, tfm4), tfm2)
|
715 |
+
assert p10.args == (tfm1, MIMOSeries(tfm3, tfm4), tfm2)
|
716 |
+
assert p10.var == s
|
717 |
+
assert p10.shape == (p10.num_outputs, p10.num_inputs) == (3, 1)
|
718 |
+
|
719 |
+
p11 = MIMOParallel(tfm2, tfm1, tfm4)
|
720 |
+
assert p11.args == (tfm2, tfm1, tfm4)
|
721 |
+
assert p11.shape == (p11.num_outputs, p11.num_inputs) == (3, 1)
|
722 |
+
|
723 |
+
p12 = MIMOParallel(tfm6, tfm5)
|
724 |
+
assert p12.args == (tfm6, tfm5)
|
725 |
+
assert p12.shape == (p12.num_outputs, p12.num_inputs) == (2, 2)
|
726 |
+
|
727 |
+
p13 = MIMOParallel(tfm2, tfm4, MIMOSeries(-tfm3, tfm4), -tfm4)
|
728 |
+
assert p13.args == (tfm2, tfm4, MIMOSeries(-tfm3, tfm4), -tfm4)
|
729 |
+
assert p13.shape == (p13.num_outputs, p13.num_inputs) == (3, 1)
|
730 |
+
|
731 |
+
# arg cannot be empty tuple.
|
732 |
+
raises(TypeError, lambda: MIMOParallel(()))
|
733 |
+
|
734 |
+
# arg cannot contain SISO as well as MIMO systems.
|
735 |
+
raises(TypeError, lambda: MIMOParallel(tfm1, tfm2, TF1))
|
736 |
+
|
737 |
+
# all TFMs must have same shapes.
|
738 |
+
raises(TypeError, lambda: MIMOParallel(tfm1, tfm3, tfm4))
|
739 |
+
|
740 |
+
# all TFMs must be using the same complex variable.
|
741 |
+
raises(ValueError, lambda: MIMOParallel(tfm3, tfm7))
|
742 |
+
|
743 |
+
# Number or expression not allowed in the arguments.
|
744 |
+
raises(TypeError, lambda: MIMOParallel(2, tfm1, tfm4))
|
745 |
+
raises(TypeError, lambda: MIMOParallel(s**2 + p*s, -tfm4, tfm2))
|
746 |
+
|
747 |
+
|
748 |
+
def test_Parallel_functions():
|
749 |
+
tf1 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
750 |
+
tf2 = TransferFunction(k, 1, s)
|
751 |
+
tf3 = TransferFunction(a2*p - s, a2*s + p, s)
|
752 |
+
tf4 = TransferFunction(a0*p + p**a1 - s, p, p)
|
753 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
754 |
+
|
755 |
+
assert tf1 + tf2 + tf3 == Parallel(tf1, tf2, tf3)
|
756 |
+
assert tf1 + tf2 + tf3 + tf5 == Parallel(tf1, tf2, tf3, tf5)
|
757 |
+
assert tf1 + tf2 - tf3 - tf5 == Parallel(tf1, tf2, -tf3, -tf5)
|
758 |
+
assert tf1 + tf2*tf3 == Parallel(tf1, Series(tf2, tf3))
|
759 |
+
assert tf1 - tf2*tf3 == Parallel(tf1, -Series(tf2,tf3))
|
760 |
+
assert -tf1 - tf2 == Parallel(-tf1, -tf2)
|
761 |
+
assert -(tf1 + tf2) == Series(TransferFunction(-1, 1, s), Parallel(tf1, tf2))
|
762 |
+
assert (tf2 + tf3)*tf1 == Series(Parallel(tf2, tf3), tf1)
|
763 |
+
assert (tf1 + tf2)*(tf3*tf5) == Series(Parallel(tf1, tf2), tf3, tf5)
|
764 |
+
assert -(tf2 + tf3)*-tf5 == Series(TransferFunction(-1, 1, s), Parallel(tf2, tf3), -tf5)
|
765 |
+
assert tf2 + tf3 + tf2*tf1 + tf5 == Parallel(tf2, tf3, Series(tf2, tf1), tf5)
|
766 |
+
assert tf2 + tf3 + tf2*tf1 - tf3 == Parallel(tf2, tf3, Series(tf2, tf1), -tf3)
|
767 |
+
assert (tf1 + tf2 + tf5)*(tf3 + tf5) == Series(Parallel(tf1, tf2, tf5), Parallel(tf3, tf5))
|
768 |
+
raises(ValueError, lambda: tf1 + tf2 + tf4)
|
769 |
+
raises(ValueError, lambda: tf1 - tf2*tf4)
|
770 |
+
raises(ValueError, lambda: tf3 + Matrix([1, 2, 3]))
|
771 |
+
|
772 |
+
# evaluate=True -> doit()
|
773 |
+
assert Parallel(tf1, tf2, evaluate=True) == Parallel(tf1, tf2).doit() == \
|
774 |
+
TransferFunction(k*(s**2 + 2*s*wn*zeta + wn**2) + 1, s**2 + 2*s*wn*zeta + wn**2, s)
|
775 |
+
assert Parallel(tf1, tf2, Series(-tf1, tf3), evaluate=True) == \
|
776 |
+
Parallel(tf1, tf2, Series(-tf1, tf3)).doit() == TransferFunction(k*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2)**2 + \
|
777 |
+
(-a2*p + s)*(s**2 + 2*s*wn*zeta + wn**2) + (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), (a2*s + p)*(s**2 + \
|
778 |
+
2*s*wn*zeta + wn**2)**2, s)
|
779 |
+
assert Parallel(tf2, tf1, -tf3, evaluate=True) == Parallel(tf2, tf1, -tf3).doit() == \
|
780 |
+
TransferFunction(a2*s + k*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) + p + (-a2*p + s)*(s**2 + 2*s*wn*zeta + wn**2) \
|
781 |
+
, (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
782 |
+
assert not Parallel(tf1, -tf2, evaluate=False) == Parallel(tf1, -tf2).doit()
|
783 |
+
|
784 |
+
assert Parallel(Series(tf1, tf2), Series(tf2, tf3)).doit() == \
|
785 |
+
TransferFunction(k*(a2*p - s)*(s**2 + 2*s*wn*zeta + wn**2) + k*(a2*s + p), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
786 |
+
assert Parallel(-tf1, -tf2, -tf3).doit() == \
|
787 |
+
TransferFunction(-a2*s - k*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) - p + (-a2*p + s)*(s**2 + 2*s*wn*zeta + wn**2), \
|
788 |
+
(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
789 |
+
assert -Parallel(tf1, tf2, tf3).doit() == \
|
790 |
+
TransferFunction(-a2*s - k*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) - p - (a2*p - s)*(s**2 + 2*s*wn*zeta + wn**2), \
|
791 |
+
(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
792 |
+
assert Parallel(tf2, tf3, Series(tf2, -tf1), tf3).doit() == \
|
793 |
+
TransferFunction(k*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) - k*(a2*s + p) + (2*a2*p - 2*s)*(s**2 + 2*s*wn*zeta \
|
794 |
+
+ wn**2), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
795 |
+
|
796 |
+
assert Parallel(tf1, tf2).rewrite(TransferFunction) == \
|
797 |
+
TransferFunction(k*(s**2 + 2*s*wn*zeta + wn**2) + 1, s**2 + 2*s*wn*zeta + wn**2, s)
|
798 |
+
assert Parallel(tf2, tf1, -tf3).rewrite(TransferFunction) == \
|
799 |
+
TransferFunction(a2*s + k*(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2) + p + (-a2*p + s)*(s**2 + 2*s*wn*zeta + \
|
800 |
+
wn**2), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
801 |
+
|
802 |
+
assert Parallel(tf1, Parallel(tf2, tf3)) == Parallel(tf1, tf2, tf3) == Parallel(Parallel(tf1, tf2), tf3)
|
803 |
+
|
804 |
+
P1 = Parallel(Series(tf1, tf2), Series(tf2, tf3))
|
805 |
+
assert P1.is_proper
|
806 |
+
assert not P1.is_strictly_proper
|
807 |
+
assert P1.is_biproper
|
808 |
+
|
809 |
+
P2 = Parallel(tf1, -tf2, -tf3)
|
810 |
+
assert P2.is_proper
|
811 |
+
assert not P2.is_strictly_proper
|
812 |
+
assert P2.is_biproper
|
813 |
+
|
814 |
+
P3 = Parallel(tf1, -tf2, Series(tf1, tf3))
|
815 |
+
assert P3.is_proper
|
816 |
+
assert not P3.is_strictly_proper
|
817 |
+
assert P3.is_biproper
|
818 |
+
|
819 |
+
|
820 |
+
def test_MIMOParallel_functions():
|
821 |
+
tf4 = TransferFunction(a0*p + p**a1 - s, p, p)
|
822 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
823 |
+
|
824 |
+
tfm1 = TransferFunctionMatrix([[TF1], [TF2], [TF3]])
|
825 |
+
tfm2 = TransferFunctionMatrix([[-TF2], [tf5], [-TF1]])
|
826 |
+
tfm3 = TransferFunctionMatrix([[tf5], [-tf5], [TF2]])
|
827 |
+
tfm4 = TransferFunctionMatrix([[TF2, -tf5], [TF1, tf5]])
|
828 |
+
tfm5 = TransferFunctionMatrix([[TF1, TF2], [TF3, -tf5]])
|
829 |
+
tfm6 = TransferFunctionMatrix([[-TF2]])
|
830 |
+
tfm7 = TransferFunctionMatrix([[tf4], [-tf4], [tf4]])
|
831 |
+
|
832 |
+
assert tfm1 + tfm2 + tfm3 == MIMOParallel(tfm1, tfm2, tfm3) == MIMOParallel(MIMOParallel(tfm1, tfm2), tfm3)
|
833 |
+
assert tfm2 - tfm1 - tfm3 == MIMOParallel(tfm2, -tfm1, -tfm3)
|
834 |
+
assert tfm2 - tfm3 + (-tfm1*tfm6*-tfm6) == MIMOParallel(tfm2, -tfm3, MIMOSeries(-tfm6, tfm6, -tfm1))
|
835 |
+
assert tfm1 + tfm1 - (-tfm1*tfm6) == MIMOParallel(tfm1, tfm1, -MIMOSeries(tfm6, -tfm1))
|
836 |
+
assert tfm2 - tfm3 - tfm1 + tfm2 == MIMOParallel(tfm2, -tfm3, -tfm1, tfm2)
|
837 |
+
assert tfm1 + tfm2 - tfm3 - tfm1 == MIMOParallel(tfm1, tfm2, -tfm3, -tfm1)
|
838 |
+
raises(ValueError, lambda: tfm1 + tfm2 + TF2)
|
839 |
+
raises(TypeError, lambda: tfm1 - tfm2 - a1)
|
840 |
+
raises(TypeError, lambda: tfm2 - tfm3 - (s - 1))
|
841 |
+
raises(TypeError, lambda: -tfm3 - tfm2 - 9)
|
842 |
+
raises(TypeError, lambda: (1 - p**3) - tfm3 - tfm2)
|
843 |
+
# All TFMs must use the same complex var. tfm7 uses 'p'.
|
844 |
+
raises(ValueError, lambda: tfm3 - tfm2 - tfm7)
|
845 |
+
raises(ValueError, lambda: tfm2 - tfm1 + tfm7)
|
846 |
+
# (tfm1 +/- tfm2) has (3, 1) shape while tfm4 has (2, 2) shape.
|
847 |
+
raises(TypeError, lambda: tfm1 + tfm2 + tfm4)
|
848 |
+
raises(TypeError, lambda: (tfm1 - tfm2) - tfm4)
|
849 |
+
|
850 |
+
assert (tfm1 + tfm2)*tfm6 == MIMOSeries(tfm6, MIMOParallel(tfm1, tfm2))
|
851 |
+
assert (tfm2 - tfm3)*tfm6*-tfm6 == MIMOSeries(-tfm6, tfm6, MIMOParallel(tfm2, -tfm3))
|
852 |
+
assert (tfm2 - tfm1 - tfm3)*(tfm6 + tfm6) == MIMOSeries(MIMOParallel(tfm6, tfm6), MIMOParallel(tfm2, -tfm1, -tfm3))
|
853 |
+
raises(ValueError, lambda: (tfm4 + tfm5)*TF1)
|
854 |
+
raises(TypeError, lambda: (tfm2 - tfm3)*a2)
|
855 |
+
raises(TypeError, lambda: (tfm3 + tfm2)*(s - 6))
|
856 |
+
raises(TypeError, lambda: (tfm1 + tfm2 + tfm3)*0)
|
857 |
+
raises(TypeError, lambda: (1 - p**3)*(tfm1 + tfm3))
|
858 |
+
|
859 |
+
# (tfm3 - tfm2) has (3, 1) shape while tfm4*tfm5 has (2, 2) shape.
|
860 |
+
raises(ValueError, lambda: (tfm3 - tfm2)*tfm4*tfm5)
|
861 |
+
# (tfm1 - tfm2) has (3, 1) shape while tfm5 has (2, 2) shape.
|
862 |
+
raises(ValueError, lambda: (tfm1 - tfm2)*tfm5)
|
863 |
+
|
864 |
+
# TFM in the arguments.
|
865 |
+
assert (MIMOParallel(tfm1, tfm2, evaluate=True) == MIMOParallel(tfm1, tfm2).doit()
|
866 |
+
== MIMOParallel(tfm1, tfm2).rewrite(TransferFunctionMatrix)
|
867 |
+
== TransferFunctionMatrix(((TransferFunction(-k*(s**2 + 2*s*wn*zeta + wn**2) + 1, s**2 + 2*s*wn*zeta + wn**2, s),), \
|
868 |
+
(TransferFunction(-a0 + a1*s**2 + a2*s + k*(a0 + s), a0 + s, s),), (TransferFunction(-a2*s - p + (a2*p - s)* \
|
869 |
+
(s**2 + 2*s*wn*zeta + wn**2), (a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s),))))
|
870 |
+
|
871 |
+
|
872 |
+
def test_Feedback_construction():
|
873 |
+
tf1 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
874 |
+
tf2 = TransferFunction(k, 1, s)
|
875 |
+
tf3 = TransferFunction(a2*p - s, a2*s + p, s)
|
876 |
+
tf4 = TransferFunction(a0*p + p**a1 - s, p, p)
|
877 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
878 |
+
tf6 = TransferFunction(s - p, p + s, p)
|
879 |
+
|
880 |
+
f1 = Feedback(TransferFunction(1, 1, s), tf1*tf2*tf3)
|
881 |
+
assert f1.args == (TransferFunction(1, 1, s), Series(tf1, tf2, tf3), -1)
|
882 |
+
assert f1.sys1 == TransferFunction(1, 1, s)
|
883 |
+
assert f1.sys2 == Series(tf1, tf2, tf3)
|
884 |
+
assert f1.var == s
|
885 |
+
|
886 |
+
f2 = Feedback(tf1, tf2*tf3)
|
887 |
+
assert f2.args == (tf1, Series(tf2, tf3), -1)
|
888 |
+
assert f2.sys1 == tf1
|
889 |
+
assert f2.sys2 == Series(tf2, tf3)
|
890 |
+
assert f2.var == s
|
891 |
+
|
892 |
+
f3 = Feedback(tf1*tf2, tf5)
|
893 |
+
assert f3.args == (Series(tf1, tf2), tf5, -1)
|
894 |
+
assert f3.sys1 == Series(tf1, tf2)
|
895 |
+
|
896 |
+
f4 = Feedback(tf4, tf6)
|
897 |
+
assert f4.args == (tf4, tf6, -1)
|
898 |
+
assert f4.sys1 == tf4
|
899 |
+
assert f4.var == p
|
900 |
+
|
901 |
+
f5 = Feedback(tf5, TransferFunction(1, 1, s))
|
902 |
+
assert f5.args == (tf5, TransferFunction(1, 1, s), -1)
|
903 |
+
assert f5.var == s
|
904 |
+
assert f5 == Feedback(tf5) # When sys2 is not passed explicitly, it is assumed to be unit tf.
|
905 |
+
|
906 |
+
f6 = Feedback(TransferFunction(1, 1, p), tf4)
|
907 |
+
assert f6.args == (TransferFunction(1, 1, p), tf4, -1)
|
908 |
+
assert f6.var == p
|
909 |
+
|
910 |
+
f7 = -Feedback(tf4*tf6, TransferFunction(1, 1, p))
|
911 |
+
assert f7.args == (Series(TransferFunction(-1, 1, p), Series(tf4, tf6)), -TransferFunction(1, 1, p), -1)
|
912 |
+
assert f7.sys1 == Series(TransferFunction(-1, 1, p), Series(tf4, tf6))
|
913 |
+
|
914 |
+
# denominator can't be a Parallel instance
|
915 |
+
raises(TypeError, lambda: Feedback(tf1, tf2 + tf3))
|
916 |
+
raises(TypeError, lambda: Feedback(tf1, Matrix([1, 2, 3])))
|
917 |
+
raises(TypeError, lambda: Feedback(TransferFunction(1, 1, s), s - 1))
|
918 |
+
raises(TypeError, lambda: Feedback(1, 1))
|
919 |
+
# raises(ValueError, lambda: Feedback(TransferFunction(1, 1, s), TransferFunction(1, 1, s)))
|
920 |
+
raises(ValueError, lambda: Feedback(tf2, tf4*tf5))
|
921 |
+
raises(ValueError, lambda: Feedback(tf2, tf1, 1.5)) # `sign` can only be -1 or 1
|
922 |
+
raises(ValueError, lambda: Feedback(tf1, -tf1**-1)) # denominator can't be zero
|
923 |
+
raises(ValueError, lambda: Feedback(tf4, tf5)) # Both systems should use the same `var`
|
924 |
+
|
925 |
+
|
926 |
+
def test_Feedback_functions():
|
927 |
+
tf = TransferFunction(1, 1, s)
|
928 |
+
tf1 = TransferFunction(1, s**2 + 2*zeta*wn*s + wn**2, s)
|
929 |
+
tf2 = TransferFunction(k, 1, s)
|
930 |
+
tf3 = TransferFunction(a2*p - s, a2*s + p, s)
|
931 |
+
tf4 = TransferFunction(a0*p + p**a1 - s, p, p)
|
932 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
933 |
+
tf6 = TransferFunction(s - p, p + s, p)
|
934 |
+
|
935 |
+
assert tf / (tf + tf1) == Feedback(tf, tf1)
|
936 |
+
assert tf / (tf + tf1*tf2*tf3) == Feedback(tf, tf1*tf2*tf3)
|
937 |
+
assert tf1 / (tf + tf1*tf2*tf3) == Feedback(tf1, tf2*tf3)
|
938 |
+
assert (tf1*tf2) / (tf + tf1*tf2) == Feedback(tf1*tf2, tf)
|
939 |
+
assert (tf1*tf2) / (tf + tf1*tf2*tf5) == Feedback(tf1*tf2, tf5)
|
940 |
+
assert (tf1*tf2) / (tf + tf1*tf2*tf5*tf3) in (Feedback(tf1*tf2, tf5*tf3), Feedback(tf1*tf2, tf3*tf5))
|
941 |
+
assert tf4 / (TransferFunction(1, 1, p) + tf4*tf6) == Feedback(tf4, tf6)
|
942 |
+
assert tf5 / (tf + tf5) == Feedback(tf5, tf)
|
943 |
+
|
944 |
+
raises(TypeError, lambda: tf1*tf2*tf3 / (1 + tf1*tf2*tf3))
|
945 |
+
raises(ValueError, lambda: tf1*tf2*tf3 / tf3*tf5)
|
946 |
+
raises(ValueError, lambda: tf2*tf3 / (tf + tf2*tf3*tf4))
|
947 |
+
|
948 |
+
assert Feedback(tf, tf1*tf2*tf3).doit() == \
|
949 |
+
TransferFunction((a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), k*(a2*p - s) + \
|
950 |
+
(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), s)
|
951 |
+
assert Feedback(tf, tf1*tf2*tf3).sensitivity == \
|
952 |
+
1/(k*(a2*p - s)/((a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2)) + 1)
|
953 |
+
assert Feedback(tf1, tf2*tf3).doit() == \
|
954 |
+
TransferFunction((a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2), (k*(a2*p - s) + \
|
955 |
+
(a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2))*(s**2 + 2*s*wn*zeta + wn**2), s)
|
956 |
+
assert Feedback(tf1, tf2*tf3).sensitivity == \
|
957 |
+
1/(k*(a2*p - s)/((a2*s + p)*(s**2 + 2*s*wn*zeta + wn**2)) + 1)
|
958 |
+
assert Feedback(tf1*tf2, tf5).doit() == \
|
959 |
+
TransferFunction(k*(a0 + s)*(s**2 + 2*s*wn*zeta + wn**2), (k*(-a0 + a1*s**2 + a2*s) + \
|
960 |
+
(a0 + s)*(s**2 + 2*s*wn*zeta + wn**2))*(s**2 + 2*s*wn*zeta + wn**2), s)
|
961 |
+
assert Feedback(tf1*tf2, tf5, 1).sensitivity == \
|
962 |
+
1/(-k*(-a0 + a1*s**2 + a2*s)/((a0 + s)*(s**2 + 2*s*wn*zeta + wn**2)) + 1)
|
963 |
+
assert Feedback(tf4, tf6).doit() == \
|
964 |
+
TransferFunction(p*(p + s)*(a0*p + p**a1 - s), p*(p*(p + s) + (-p + s)*(a0*p + p**a1 - s)), p)
|
965 |
+
assert -Feedback(tf4*tf6, TransferFunction(1, 1, p)).doit() == \
|
966 |
+
TransferFunction(-p*(-p + s)*(p + s)*(a0*p + p**a1 - s), p*(p + s)*(p*(p + s) + (-p + s)*(a0*p + p**a1 - s)), p)
|
967 |
+
assert Feedback(tf, tf).doit() == TransferFunction(1, 2, s)
|
968 |
+
|
969 |
+
assert Feedback(tf1, tf2*tf5).rewrite(TransferFunction) == \
|
970 |
+
TransferFunction((a0 + s)*(s**2 + 2*s*wn*zeta + wn**2), (k*(-a0 + a1*s**2 + a2*s) + \
|
971 |
+
(a0 + s)*(s**2 + 2*s*wn*zeta + wn**2))*(s**2 + 2*s*wn*zeta + wn**2), s)
|
972 |
+
assert Feedback(TransferFunction(1, 1, p), tf4).rewrite(TransferFunction) == \
|
973 |
+
TransferFunction(p, a0*p + p + p**a1 - s, p)
|
974 |
+
|
975 |
+
|
976 |
+
def test_MIMOFeedback_construction():
|
977 |
+
tf1 = TransferFunction(1, s, s)
|
978 |
+
tf2 = TransferFunction(s, s**3 - 1, s)
|
979 |
+
tf3 = TransferFunction(s, s + 1, s)
|
980 |
+
tf4 = TransferFunction(s, s**2 + 1, s)
|
981 |
+
|
982 |
+
tfm_1 = TransferFunctionMatrix([[tf1, tf2], [tf3, tf4]])
|
983 |
+
tfm_2 = TransferFunctionMatrix([[tf2, tf3], [tf4, tf1]])
|
984 |
+
tfm_3 = TransferFunctionMatrix([[tf3, tf4], [tf1, tf2]])
|
985 |
+
|
986 |
+
f1 = MIMOFeedback(tfm_1, tfm_2)
|
987 |
+
assert f1.args == (tfm_1, tfm_2, -1)
|
988 |
+
assert f1.sys1 == tfm_1
|
989 |
+
assert f1.sys2 == tfm_2
|
990 |
+
assert f1.var == s
|
991 |
+
assert f1.sign == -1
|
992 |
+
assert -(-f1) == f1
|
993 |
+
|
994 |
+
f2 = MIMOFeedback(tfm_2, tfm_1, 1)
|
995 |
+
assert f2.args == (tfm_2, tfm_1, 1)
|
996 |
+
assert f2.sys1 == tfm_2
|
997 |
+
assert f2.sys2 == tfm_1
|
998 |
+
assert f2.var == s
|
999 |
+
assert f2.sign == 1
|
1000 |
+
|
1001 |
+
f3 = MIMOFeedback(tfm_1, MIMOSeries(tfm_3, tfm_2))
|
1002 |
+
assert f3.args == (tfm_1, MIMOSeries(tfm_3, tfm_2), -1)
|
1003 |
+
assert f3.sys1 == tfm_1
|
1004 |
+
assert f3.sys2 == MIMOSeries(tfm_3, tfm_2)
|
1005 |
+
assert f3.var == s
|
1006 |
+
assert f3.sign == -1
|
1007 |
+
|
1008 |
+
mat = Matrix([[1, 1/s], [0, 1]])
|
1009 |
+
sys1 = controller = TransferFunctionMatrix.from_Matrix(mat, s)
|
1010 |
+
f4 = MIMOFeedback(sys1, controller)
|
1011 |
+
assert f4.args == (sys1, controller, -1)
|
1012 |
+
assert f4.sys1 == f4.sys2 == sys1
|
1013 |
+
|
1014 |
+
|
1015 |
+
def test_MIMOFeedback_errors():
|
1016 |
+
tf1 = TransferFunction(1, s, s)
|
1017 |
+
tf2 = TransferFunction(s, s**3 - 1, s)
|
1018 |
+
tf3 = TransferFunction(s, s - 1, s)
|
1019 |
+
tf4 = TransferFunction(s, s**2 + 1, s)
|
1020 |
+
tf5 = TransferFunction(1, 1, s)
|
1021 |
+
tf6 = TransferFunction(-1, s - 1, s)
|
1022 |
+
|
1023 |
+
tfm_1 = TransferFunctionMatrix([[tf1, tf2], [tf3, tf4]])
|
1024 |
+
tfm_2 = TransferFunctionMatrix([[tf2, tf3], [tf4, tf1]])
|
1025 |
+
tfm_3 = TransferFunctionMatrix.from_Matrix(eye(2), var=s)
|
1026 |
+
tfm_4 = TransferFunctionMatrix([[tf1, tf5], [tf5, tf5]])
|
1027 |
+
tfm_5 = TransferFunctionMatrix([[-tf3, tf3], [tf3, tf6]])
|
1028 |
+
# tfm_4 is inverse of tfm_5. Therefore tfm_5*tfm_4 = I
|
1029 |
+
tfm_6 = TransferFunctionMatrix([[-tf3]])
|
1030 |
+
tfm_7 = TransferFunctionMatrix([[tf3, tf4]])
|
1031 |
+
|
1032 |
+
# Unsupported Types
|
1033 |
+
raises(TypeError, lambda: MIMOFeedback(tf1, tf2))
|
1034 |
+
raises(TypeError, lambda: MIMOFeedback(MIMOParallel(tfm_1, tfm_2), tfm_3))
|
1035 |
+
# Shape Errors
|
1036 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_1, tfm_6, 1))
|
1037 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_7, tfm_7))
|
1038 |
+
# sign not 1/-1
|
1039 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_1, tfm_2, -2))
|
1040 |
+
# Non-Invertible Systems
|
1041 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_5, tfm_4, 1))
|
1042 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_4, -tfm_5))
|
1043 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_3, tfm_3, 1))
|
1044 |
+
# Variable not same in both the systems
|
1045 |
+
tfm_8 = TransferFunctionMatrix.from_Matrix(eye(2), var=p)
|
1046 |
+
raises(ValueError, lambda: MIMOFeedback(tfm_1, tfm_8, 1))
|
1047 |
+
|
1048 |
+
|
1049 |
+
def test_MIMOFeedback_functions():
|
1050 |
+
tf1 = TransferFunction(1, s, s)
|
1051 |
+
tf2 = TransferFunction(s, s - 1, s)
|
1052 |
+
tf3 = TransferFunction(1, 1, s)
|
1053 |
+
tf4 = TransferFunction(-1, s - 1, s)
|
1054 |
+
|
1055 |
+
tfm_1 = TransferFunctionMatrix.from_Matrix(eye(2), var=s)
|
1056 |
+
tfm_2 = TransferFunctionMatrix([[tf1, tf3], [tf3, tf3]])
|
1057 |
+
tfm_3 = TransferFunctionMatrix([[-tf2, tf2], [tf2, tf4]])
|
1058 |
+
tfm_4 = TransferFunctionMatrix([[tf1, tf2], [-tf2, tf1]])
|
1059 |
+
|
1060 |
+
# sensitivity, doit(), rewrite()
|
1061 |
+
F_1 = MIMOFeedback(tfm_2, tfm_3)
|
1062 |
+
F_2 = MIMOFeedback(tfm_2, MIMOSeries(tfm_4, -tfm_1), 1)
|
1063 |
+
|
1064 |
+
assert F_1.sensitivity == Matrix([[S.Half, 0], [0, S.Half]])
|
1065 |
+
assert F_2.sensitivity == Matrix([[(-2*s**4 + s**2)/(s**2 - s + 1),
|
1066 |
+
(2*s**3 - s**2)/(s**2 - s + 1)], [-s**2, s]])
|
1067 |
+
|
1068 |
+
assert F_1.doit() == \
|
1069 |
+
TransferFunctionMatrix(((TransferFunction(1, 2*s, s),
|
1070 |
+
TransferFunction(1, 2, s)), (TransferFunction(1, 2, s),
|
1071 |
+
TransferFunction(1, 2, s)))) == F_1.rewrite(TransferFunctionMatrix)
|
1072 |
+
assert F_2.doit(cancel=False, expand=True) == \
|
1073 |
+
TransferFunctionMatrix(((TransferFunction(-s**5 + 2*s**4 - 2*s**3 + s**2, s**5 - 2*s**4 + 3*s**3 - 2*s**2 + s, s),
|
1074 |
+
TransferFunction(-2*s**4 + 2*s**3, s**2 - s + 1, s)), (TransferFunction(0, 1, s), TransferFunction(-s**2 + s, 1, s))))
|
1075 |
+
assert F_2.doit(cancel=False) == \
|
1076 |
+
TransferFunctionMatrix(((TransferFunction(s*(2*s**3 - s**2)*(s**2 - s + 1) + \
|
1077 |
+
(-2*s**4 + s**2)*(s**2 - s + 1), s*(s**2 - s + 1)**2, s), TransferFunction(-2*s**4 + 2*s**3, s**2 - s + 1, s)),
|
1078 |
+
(TransferFunction(0, 1, s), TransferFunction(-s**2 + s, 1, s))))
|
1079 |
+
assert F_2.doit() == \
|
1080 |
+
TransferFunctionMatrix(((TransferFunction(s*(-2*s**2 + s*(2*s - 1) + 1), s**2 - s + 1, s),
|
1081 |
+
TransferFunction(-2*s**3*(s - 1), s**2 - s + 1, s)), (TransferFunction(0, 1, s), TransferFunction(s*(1 - s), 1, s))))
|
1082 |
+
assert F_2.doit(expand=True) == \
|
1083 |
+
TransferFunctionMatrix(((TransferFunction(-s**2 + s, s**2 - s + 1, s), TransferFunction(-2*s**4 + 2*s**3, s**2 - s + 1, s)),
|
1084 |
+
(TransferFunction(0, 1, s), TransferFunction(-s**2 + s, 1, s))))
|
1085 |
+
|
1086 |
+
assert -(F_1.doit()) == (-F_1).doit() # First negating then calculating vs calculating then negating.
|
1087 |
+
|
1088 |
+
|
1089 |
+
def test_TransferFunctionMatrix_construction():
|
1090 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
1091 |
+
tf4 = TransferFunction(a0*p + p**a1 - s, p, p)
|
1092 |
+
|
1093 |
+
tfm3_ = TransferFunctionMatrix([[-TF3]])
|
1094 |
+
assert tfm3_.shape == (tfm3_.num_outputs, tfm3_.num_inputs) == (1, 1)
|
1095 |
+
assert tfm3_.args == Tuple(Tuple(Tuple(-TF3)))
|
1096 |
+
assert tfm3_.var == s
|
1097 |
+
|
1098 |
+
tfm5 = TransferFunctionMatrix([[TF1, -TF2], [TF3, tf5]])
|
1099 |
+
assert tfm5.shape == (tfm5.num_outputs, tfm5.num_inputs) == (2, 2)
|
1100 |
+
assert tfm5.args == Tuple(Tuple(Tuple(TF1, -TF2), Tuple(TF3, tf5)))
|
1101 |
+
assert tfm5.var == s
|
1102 |
+
|
1103 |
+
tfm7 = TransferFunctionMatrix([[TF1, TF2], [TF3, -tf5], [-tf5, TF2]])
|
1104 |
+
assert tfm7.shape == (tfm7.num_outputs, tfm7.num_inputs) == (3, 2)
|
1105 |
+
assert tfm7.args == Tuple(Tuple(Tuple(TF1, TF2), Tuple(TF3, -tf5), Tuple(-tf5, TF2)))
|
1106 |
+
assert tfm7.var == s
|
1107 |
+
|
1108 |
+
# all transfer functions will use the same complex variable. tf4 uses 'p'.
|
1109 |
+
raises(ValueError, lambda: TransferFunctionMatrix([[TF1], [TF2], [tf4]]))
|
1110 |
+
raises(ValueError, lambda: TransferFunctionMatrix([[TF1, tf4], [TF3, tf5]]))
|
1111 |
+
|
1112 |
+
# length of all the lists in the TFM should be equal.
|
1113 |
+
raises(ValueError, lambda: TransferFunctionMatrix([[TF1], [TF3, tf5]]))
|
1114 |
+
raises(ValueError, lambda: TransferFunctionMatrix([[TF1, TF3], [tf5]]))
|
1115 |
+
|
1116 |
+
# lists should only support transfer functions in them.
|
1117 |
+
raises(TypeError, lambda: TransferFunctionMatrix([[TF1, TF2], [TF3, Matrix([1, 2])]]))
|
1118 |
+
raises(TypeError, lambda: TransferFunctionMatrix([[TF1, Matrix([1, 2])], [TF3, TF2]]))
|
1119 |
+
|
1120 |
+
# `arg` should strictly be nested list of TransferFunction
|
1121 |
+
raises(ValueError, lambda: TransferFunctionMatrix([TF1, TF2, tf5]))
|
1122 |
+
raises(ValueError, lambda: TransferFunctionMatrix([TF1]))
|
1123 |
+
|
1124 |
+
def test_TransferFunctionMatrix_functions():
|
1125 |
+
tf5 = TransferFunction(a1*s**2 + a2*s - a0, s + a0, s)
|
1126 |
+
|
1127 |
+
# Classmethod (from_matrix)
|
1128 |
+
|
1129 |
+
mat_1 = ImmutableMatrix([
|
1130 |
+
[s*(s + 1)*(s - 3)/(s**4 + 1), 2],
|
1131 |
+
[p, p*(s + 1)/(s*(s**1 + 1))]
|
1132 |
+
])
|
1133 |
+
mat_2 = ImmutableMatrix([[(2*s + 1)/(s**2 - 9)]])
|
1134 |
+
mat_3 = ImmutableMatrix([[1, 2], [3, 4]])
|
1135 |
+
assert TransferFunctionMatrix.from_Matrix(mat_1, s) == \
|
1136 |
+
TransferFunctionMatrix([[TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(2, 1, s)],
|
1137 |
+
[TransferFunction(p, 1, s), TransferFunction(p, s, s)]])
|
1138 |
+
assert TransferFunctionMatrix.from_Matrix(mat_2, s) == \
|
1139 |
+
TransferFunctionMatrix([[TransferFunction(2*s + 1, s**2 - 9, s)]])
|
1140 |
+
assert TransferFunctionMatrix.from_Matrix(mat_3, p) == \
|
1141 |
+
TransferFunctionMatrix([[TransferFunction(1, 1, p), TransferFunction(2, 1, p)],
|
1142 |
+
[TransferFunction(3, 1, p), TransferFunction(4, 1, p)]])
|
1143 |
+
|
1144 |
+
# Negating a TFM
|
1145 |
+
|
1146 |
+
tfm1 = TransferFunctionMatrix([[TF1], [TF2]])
|
1147 |
+
assert -tfm1 == TransferFunctionMatrix([[-TF1], [-TF2]])
|
1148 |
+
|
1149 |
+
tfm2 = TransferFunctionMatrix([[TF1, TF2, TF3], [tf5, -TF1, -TF3]])
|
1150 |
+
assert -tfm2 == TransferFunctionMatrix([[-TF1, -TF2, -TF3], [-tf5, TF1, TF3]])
|
1151 |
+
|
1152 |
+
# subs()
|
1153 |
+
|
1154 |
+
H_1 = TransferFunctionMatrix.from_Matrix(mat_1, s)
|
1155 |
+
H_2 = TransferFunctionMatrix([[TransferFunction(a*p*s, k*s**2, s), TransferFunction(p*s, k*(s**2 - a), s)]])
|
1156 |
+
assert H_1.subs(p, 1) == TransferFunctionMatrix([[TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(2, 1, s)], [TransferFunction(1, 1, s), TransferFunction(1, s, s)]])
|
1157 |
+
assert H_1.subs({p: 1}) == TransferFunctionMatrix([[TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(2, 1, s)], [TransferFunction(1, 1, s), TransferFunction(1, s, s)]])
|
1158 |
+
assert H_1.subs({p: 1, s: 1}) == TransferFunctionMatrix([[TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(2, 1, s)], [TransferFunction(1, 1, s), TransferFunction(1, s, s)]]) # This should ignore `s` as it is `var`
|
1159 |
+
assert H_2.subs(p, 2) == TransferFunctionMatrix([[TransferFunction(2*a*s, k*s**2, s), TransferFunction(2*s, k*(-a + s**2), s)]])
|
1160 |
+
assert H_2.subs(k, 1) == TransferFunctionMatrix([[TransferFunction(a*p*s, s**2, s), TransferFunction(p*s, -a + s**2, s)]])
|
1161 |
+
assert H_2.subs(a, 0) == TransferFunctionMatrix([[TransferFunction(0, k*s**2, s), TransferFunction(p*s, k*s**2, s)]])
|
1162 |
+
assert H_2.subs({p: 1, k: 1, a: a0}) == TransferFunctionMatrix([[TransferFunction(a0*s, s**2, s), TransferFunction(s, -a0 + s**2, s)]])
|
1163 |
+
|
1164 |
+
# transpose()
|
1165 |
+
|
1166 |
+
assert H_1.transpose() == TransferFunctionMatrix([[TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(p, 1, s)], [TransferFunction(2, 1, s), TransferFunction(p, s, s)]])
|
1167 |
+
assert H_2.transpose() == TransferFunctionMatrix([[TransferFunction(a*p*s, k*s**2, s)], [TransferFunction(p*s, k*(-a + s**2), s)]])
|
1168 |
+
assert H_1.transpose().transpose() == H_1
|
1169 |
+
assert H_2.transpose().transpose() == H_2
|
1170 |
+
|
1171 |
+
# elem_poles()
|
1172 |
+
|
1173 |
+
assert H_1.elem_poles() == [[[-sqrt(2)/2 - sqrt(2)*I/2, -sqrt(2)/2 + sqrt(2)*I/2, sqrt(2)/2 - sqrt(2)*I/2, sqrt(2)/2 + sqrt(2)*I/2], []],
|
1174 |
+
[[], [0]]]
|
1175 |
+
assert H_2.elem_poles() == [[[0, 0], [sqrt(a), -sqrt(a)]]]
|
1176 |
+
assert tfm2.elem_poles() == [[[wn*(-zeta + sqrt((zeta - 1)*(zeta + 1))), wn*(-zeta - sqrt((zeta - 1)*(zeta + 1)))], [], [-p/a2]],
|
1177 |
+
[[-a0], [wn*(-zeta + sqrt((zeta - 1)*(zeta + 1))), wn*(-zeta - sqrt((zeta - 1)*(zeta + 1)))], [-p/a2]]]
|
1178 |
+
|
1179 |
+
# elem_zeros()
|
1180 |
+
|
1181 |
+
assert H_1.elem_zeros() == [[[-1, 0, 3], []], [[], []]]
|
1182 |
+
assert H_2.elem_zeros() == [[[0], [0]]]
|
1183 |
+
assert tfm2.elem_zeros() == [[[], [], [a2*p]],
|
1184 |
+
[[-a2/(2*a1) - sqrt(4*a0*a1 + a2**2)/(2*a1), -a2/(2*a1) + sqrt(4*a0*a1 + a2**2)/(2*a1)], [], [a2*p]]]
|
1185 |
+
|
1186 |
+
# doit()
|
1187 |
+
|
1188 |
+
H_3 = TransferFunctionMatrix([[Series(TransferFunction(1, s**3 - 3, s), TransferFunction(s**2 - 2*s + 5, 1, s), TransferFunction(1, s, s))]])
|
1189 |
+
H_4 = TransferFunctionMatrix([[Parallel(TransferFunction(s**3 - 3, 4*s**4 - s**2 - 2*s + 5, s), TransferFunction(4 - s**3, 4*s**4 - s**2 - 2*s + 5, s))]])
|
1190 |
+
|
1191 |
+
assert H_3.doit() == TransferFunctionMatrix([[TransferFunction(s**2 - 2*s + 5, s*(s**3 - 3), s)]])
|
1192 |
+
assert H_4.doit() == TransferFunctionMatrix([[TransferFunction(1, 4*s**4 - s**2 - 2*s + 5, s)]])
|
1193 |
+
|
1194 |
+
# _flat()
|
1195 |
+
|
1196 |
+
assert H_1._flat() == [TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(2, 1, s), TransferFunction(p, 1, s), TransferFunction(p, s, s)]
|
1197 |
+
assert H_2._flat() == [TransferFunction(a*p*s, k*s**2, s), TransferFunction(p*s, k*(-a + s**2), s)]
|
1198 |
+
assert H_3._flat() == [Series(TransferFunction(1, s**3 - 3, s), TransferFunction(s**2 - 2*s + 5, 1, s), TransferFunction(1, s, s))]
|
1199 |
+
assert H_4._flat() == [Parallel(TransferFunction(s**3 - 3, 4*s**4 - s**2 - 2*s + 5, s), TransferFunction(4 - s**3, 4*s**4 - s**2 - 2*s + 5, s))]
|
1200 |
+
|
1201 |
+
# evalf()
|
1202 |
+
|
1203 |
+
assert H_1.evalf() == \
|
1204 |
+
TransferFunctionMatrix(((TransferFunction(s*(s - 3.0)*(s + 1.0), s**4 + 1.0, s), TransferFunction(2.0, 1, s)), (TransferFunction(1.0*p, 1, s), TransferFunction(p, s, s))))
|
1205 |
+
assert H_2.subs({a:3.141, p:2.88, k:2}).evalf() == \
|
1206 |
+
TransferFunctionMatrix(((TransferFunction(4.5230399999999999494093572138808667659759521484375, s, s),
|
1207 |
+
TransferFunction(2.87999999999999989341858963598497211933135986328125*s, 2.0*s**2 - 6.282000000000000028421709430404007434844970703125, s)),))
|
1208 |
+
|
1209 |
+
# simplify()
|
1210 |
+
|
1211 |
+
H_5 = TransferFunctionMatrix([[TransferFunction(s**5 + s**3 + s, s - s**2, s),
|
1212 |
+
TransferFunction((s + 3)*(s - 1), (s - 1)*(s + 5), s)]])
|
1213 |
+
|
1214 |
+
assert H_5.simplify() == simplify(H_5) == \
|
1215 |
+
TransferFunctionMatrix(((TransferFunction(-s**4 - s**2 - 1, s - 1, s), TransferFunction(s + 3, s + 5, s)),))
|
1216 |
+
|
1217 |
+
# expand()
|
1218 |
+
|
1219 |
+
assert (H_1.expand()
|
1220 |
+
== TransferFunctionMatrix(((TransferFunction(s**3 - 2*s**2 - 3*s, s**4 + 1, s), TransferFunction(2, 1, s)),
|
1221 |
+
(TransferFunction(p, 1, s), TransferFunction(p, s, s)))))
|
1222 |
+
assert H_5.expand() == \
|
1223 |
+
TransferFunctionMatrix(((TransferFunction(s**5 + s**3 + s, -s**2 + s, s), TransferFunction(s**2 + 2*s - 3, s**2 + 4*s - 5, s)),))
|
1224 |
+
|
1225 |
+
def test_TransferFunction_bilinear():
|
1226 |
+
# simple transfer function, e.g. ohms law
|
1227 |
+
tf = TransferFunction(1, a*s+b, s)
|
1228 |
+
numZ, denZ = bilinear(tf, T)
|
1229 |
+
# discretized transfer function with coefs from tf.bilinear()
|
1230 |
+
tf_test_bilinear = TransferFunction(s*numZ[0]+numZ[1], s*denZ[0]+denZ[1], s)
|
1231 |
+
# corresponding tf with manually calculated coefs
|
1232 |
+
tf_test_manual = TransferFunction(s*T+T, s*(T*b+2*a)+T*b-2*a, s)
|
1233 |
+
|
1234 |
+
assert S.Zero == (tf_test_bilinear-tf_test_manual).simplify().num
|
1235 |
+
|
1236 |
+
def test_TransferFunction_backward_diff():
|
1237 |
+
# simple transfer function, e.g. ohms law
|
1238 |
+
tf = TransferFunction(1, a*s+b, s)
|
1239 |
+
numZ, denZ = backward_diff(tf, T)
|
1240 |
+
# discretized transfer function with coefs from tf.bilinear()
|
1241 |
+
tf_test_bilinear = TransferFunction(s*numZ[0]+numZ[1], s*denZ[0]+denZ[1], s)
|
1242 |
+
# corresponding tf with manually calculated coefs
|
1243 |
+
tf_test_manual = TransferFunction(s*T, s*(T*b+a)-a, s)
|
1244 |
+
|
1245 |
+
assert S.Zero == (tf_test_bilinear-tf_test_manual).simplify().num
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (196 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/__pycache__/test_gamma_matrices.cpython-310.pyc
ADDED
Binary file (13.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/hep/tests/test_gamma_matrices.py
ADDED
@@ -0,0 +1,427 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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eye, Matrix
|
2 |
+
from sympy.tensor.tensor import tensor_indices, TensorHead, tensor_heads, \
|
3 |
+
TensExpr, canon_bp
|
4 |
+
from sympy.physics.hep.gamma_matrices import GammaMatrix as G, LorentzIndex, \
|
5 |
+
kahane_simplify, gamma_trace, _simplify_single_line, simplify_gamma_expression
|
6 |
+
from sympy import Symbol
|
7 |
+
|
8 |
+
|
9 |
+
def _is_tensor_eq(arg1, arg2):
|
10 |
+
arg1 = canon_bp(arg1)
|
11 |
+
arg2 = canon_bp(arg2)
|
12 |
+
if isinstance(arg1, TensExpr):
|
13 |
+
return arg1.equals(arg2)
|
14 |
+
elif isinstance(arg2, TensExpr):
|
15 |
+
return arg2.equals(arg1)
|
16 |
+
return arg1 == arg2
|
17 |
+
|
18 |
+
def execute_gamma_simplify_tests_for_function(tfunc, D):
|
19 |
+
"""
|
20 |
+
Perform tests to check if sfunc is able to simplify gamma matrix expressions.
|
21 |
+
|
22 |
+
Parameters
|
23 |
+
==========
|
24 |
+
|
25 |
+
`sfunc` a function to simplify a `TIDS`, shall return the simplified `TIDS`.
|
26 |
+
`D` the number of dimension (in most cases `D=4`).
|
27 |
+
|
28 |
+
"""
|
29 |
+
|
30 |
+
mu, nu, rho, sigma = tensor_indices("mu, nu, rho, sigma", LorentzIndex)
|
31 |
+
a1, a2, a3, a4, a5, a6 = tensor_indices("a1:7", LorentzIndex)
|
32 |
+
mu11, mu12, mu21, mu31, mu32, mu41, mu51, mu52 = tensor_indices("mu11, mu12, mu21, mu31, mu32, mu41, mu51, mu52", LorentzIndex)
|
33 |
+
mu61, mu71, mu72 = tensor_indices("mu61, mu71, mu72", LorentzIndex)
|
34 |
+
m0, m1, m2, m3, m4, m5, m6 = tensor_indices("m0:7", LorentzIndex)
|
35 |
+
|
36 |
+
def g(xx, yy):
|
37 |
+
return (G(xx)*G(yy) + G(yy)*G(xx))/2
|
38 |
+
|
39 |
+
# Some examples taken from Kahane's paper, 4 dim only:
|
40 |
+
if D == 4:
|
41 |
+
t = (G(a1)*G(mu11)*G(a2)*G(mu21)*G(-a1)*G(mu31)*G(-a2))
|
42 |
+
assert _is_tensor_eq(tfunc(t), -4*G(mu11)*G(mu31)*G(mu21) - 4*G(mu31)*G(mu11)*G(mu21))
|
43 |
+
|
44 |
+
t = (G(a1)*G(mu11)*G(mu12)*\
|
45 |
+
G(a2)*G(mu21)*\
|
46 |
+
G(a3)*G(mu31)*G(mu32)*\
|
47 |
+
G(a4)*G(mu41)*\
|
48 |
+
G(-a2)*G(mu51)*G(mu52)*\
|
49 |
+
G(-a1)*G(mu61)*\
|
50 |
+
G(-a3)*G(mu71)*G(mu72)*\
|
51 |
+
G(-a4))
|
52 |
+
assert _is_tensor_eq(tfunc(t), \
|
53 |
+
16*G(mu31)*G(mu32)*G(mu72)*G(mu71)*G(mu11)*G(mu52)*G(mu51)*G(mu12)*G(mu61)*G(mu21)*G(mu41) + 16*G(mu31)*G(mu32)*G(mu72)*G(mu71)*G(mu12)*G(mu51)*G(mu52)*G(mu11)*G(mu61)*G(mu21)*G(mu41) + 16*G(mu71)*G(mu72)*G(mu32)*G(mu31)*G(mu11)*G(mu52)*G(mu51)*G(mu12)*G(mu61)*G(mu21)*G(mu41) + 16*G(mu71)*G(mu72)*G(mu32)*G(mu31)*G(mu12)*G(mu51)*G(mu52)*G(mu11)*G(mu61)*G(mu21)*G(mu41))
|
54 |
+
|
55 |
+
# Fully Lorentz-contracted expressions, these return scalars:
|
56 |
+
|
57 |
+
def add_delta(ne):
|
58 |
+
return ne * eye(4) # DiracSpinorIndex.delta(DiracSpinorIndex.auto_left, -DiracSpinorIndex.auto_right)
|
59 |
+
|
60 |
+
t = (G(mu)*G(-mu))
|
61 |
+
ts = add_delta(D)
|
62 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
63 |
+
|
64 |
+
t = (G(mu)*G(nu)*G(-mu)*G(-nu))
|
65 |
+
ts = add_delta(2*D - D**2) # -8
|
66 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
67 |
+
|
68 |
+
t = (G(mu)*G(nu)*G(-nu)*G(-mu))
|
69 |
+
ts = add_delta(D**2) # 16
|
70 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
71 |
+
|
72 |
+
t = (G(mu)*G(nu)*G(-rho)*G(-nu)*G(-mu)*G(rho))
|
73 |
+
ts = add_delta(4*D - 4*D**2 + D**3) # 16
|
74 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
75 |
+
|
76 |
+
t = (G(mu)*G(nu)*G(rho)*G(-rho)*G(-nu)*G(-mu))
|
77 |
+
ts = add_delta(D**3) # 64
|
78 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
79 |
+
|
80 |
+
t = (G(a1)*G(a2)*G(a3)*G(a4)*G(-a3)*G(-a1)*G(-a2)*G(-a4))
|
81 |
+
ts = add_delta(-8*D + 16*D**2 - 8*D**3 + D**4) # -32
|
82 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
83 |
+
|
84 |
+
t = (G(-mu)*G(-nu)*G(-rho)*G(-sigma)*G(nu)*G(mu)*G(sigma)*G(rho))
|
85 |
+
ts = add_delta(-16*D + 24*D**2 - 8*D**3 + D**4) # 64
|
86 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
87 |
+
|
88 |
+
t = (G(-mu)*G(nu)*G(-rho)*G(sigma)*G(rho)*G(-nu)*G(mu)*G(-sigma))
|
89 |
+
ts = add_delta(8*D - 12*D**2 + 6*D**3 - D**4) # -32
|
90 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
91 |
+
|
92 |
+
t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(-a3)*G(-a2)*G(-a1)*G(-a5)*G(-a4))
|
93 |
+
ts = add_delta(64*D - 112*D**2 + 60*D**3 - 12*D**4 + D**5) # 256
|
94 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
95 |
+
|
96 |
+
t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(-a3)*G(-a1)*G(-a2)*G(-a4)*G(-a5))
|
97 |
+
ts = add_delta(64*D - 120*D**2 + 72*D**3 - 16*D**4 + D**5) # -128
|
98 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
99 |
+
|
100 |
+
t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(a6)*G(-a3)*G(-a2)*G(-a1)*G(-a6)*G(-a5)*G(-a4))
|
101 |
+
ts = add_delta(416*D - 816*D**2 + 528*D**3 - 144*D**4 + 18*D**5 - D**6) # -128
|
102 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
103 |
+
|
104 |
+
t = (G(a1)*G(a2)*G(a3)*G(a4)*G(a5)*G(a6)*G(-a2)*G(-a3)*G(-a1)*G(-a6)*G(-a4)*G(-a5))
|
105 |
+
ts = add_delta(416*D - 848*D**2 + 584*D**3 - 172*D**4 + 22*D**5 - D**6) # -128
|
106 |
+
assert _is_tensor_eq(tfunc(t), ts)
|
107 |
+
|
108 |
+
# Expressions with free indices:
|
109 |
+
|
110 |
+
t = (G(mu)*G(nu)*G(rho)*G(sigma)*G(-mu))
|
111 |
+
assert _is_tensor_eq(tfunc(t), (-2*G(sigma)*G(rho)*G(nu) + (4-D)*G(nu)*G(rho)*G(sigma)))
|
112 |
+
|
113 |
+
t = (G(mu)*G(nu)*G(-mu))
|
114 |
+
assert _is_tensor_eq(tfunc(t), (2-D)*G(nu))
|
115 |
+
|
116 |
+
t = (G(mu)*G(nu)*G(rho)*G(-mu))
|
117 |
+
assert _is_tensor_eq(tfunc(t), 2*G(nu)*G(rho) + 2*G(rho)*G(nu) - (4-D)*G(nu)*G(rho))
|
118 |
+
|
119 |
+
t = 2*G(m2)*G(m0)*G(m1)*G(-m0)*G(-m1)
|
120 |
+
st = tfunc(t)
|
121 |
+
assert _is_tensor_eq(st, (D*(-2*D + 4))*G(m2))
|
122 |
+
|
123 |
+
t = G(m2)*G(m0)*G(m1)*G(-m0)*G(-m2)
|
124 |
+
st = tfunc(t)
|
125 |
+
assert _is_tensor_eq(st, ((-D + 2)**2)*G(m1))
|
126 |
+
|
127 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)*G(-m1)
|
128 |
+
st = tfunc(t)
|
129 |
+
assert _is_tensor_eq(st, (D - 4)*G(m0)*G(m2)*G(m3) + 4*G(m0)*g(m2, m3))
|
130 |
+
|
131 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)*G(-m1)*G(-m0)
|
132 |
+
st = tfunc(t)
|
133 |
+
assert _is_tensor_eq(st, ((D - 4)**2)*G(m2)*G(m3) + (8*D - 16)*g(m2, m3))
|
134 |
+
|
135 |
+
t = G(m2)*G(m0)*G(m1)*G(-m2)*G(-m0)
|
136 |
+
st = tfunc(t)
|
137 |
+
assert _is_tensor_eq(st, ((-D + 2)*(D - 4) + 4)*G(m1))
|
138 |
+
|
139 |
+
t = G(m3)*G(m1)*G(m0)*G(m2)*G(-m3)*G(-m0)*G(-m2)
|
140 |
+
st = tfunc(t)
|
141 |
+
assert _is_tensor_eq(st, (-4*D + (-D + 2)**2*(D - 4) + 8)*G(m1))
|
142 |
+
|
143 |
+
t = 2*G(m0)*G(m1)*G(m2)*G(m3)*G(-m0)
|
144 |
+
st = tfunc(t)
|
145 |
+
assert _is_tensor_eq(st, ((-2*D + 8)*G(m1)*G(m2)*G(m3) - 4*G(m3)*G(m2)*G(m1)))
|
146 |
+
|
147 |
+
t = G(m5)*G(m0)*G(m1)*G(m4)*G(m2)*G(-m4)*G(m3)*G(-m0)
|
148 |
+
st = tfunc(t)
|
149 |
+
assert _is_tensor_eq(st, (((-D + 2)*(-D + 4))*G(m5)*G(m1)*G(m2)*G(m3) + (2*D - 4)*G(m5)*G(m3)*G(m2)*G(m1)))
|
150 |
+
|
151 |
+
t = -G(m0)*G(m1)*G(m2)*G(m3)*G(-m0)*G(m4)
|
152 |
+
st = tfunc(t)
|
153 |
+
assert _is_tensor_eq(st, ((D - 4)*G(m1)*G(m2)*G(m3)*G(m4) + 2*G(m3)*G(m2)*G(m1)*G(m4)))
|
154 |
+
|
155 |
+
t = G(-m5)*G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(-m0)*G(m5)
|
156 |
+
st = tfunc(t)
|
157 |
+
|
158 |
+
result1 = ((-D + 4)**2 + 4)*G(m1)*G(m2)*G(m3)*G(m4) +\
|
159 |
+
(4*D - 16)*G(m3)*G(m2)*G(m1)*G(m4) + (4*D - 16)*G(m4)*G(m1)*G(m2)*G(m3)\
|
160 |
+
+ 4*G(m2)*G(m1)*G(m4)*G(m3) + 4*G(m3)*G(m4)*G(m1)*G(m2) +\
|
161 |
+
4*G(m4)*G(m3)*G(m2)*G(m1)
|
162 |
+
|
163 |
+
# Kahane's algorithm yields this result, which is equivalent to `result1`
|
164 |
+
# in four dimensions, but is not automatically recognized as equal:
|
165 |
+
result2 = 8*G(m1)*G(m2)*G(m3)*G(m4) + 8*G(m4)*G(m3)*G(m2)*G(m1)
|
166 |
+
|
167 |
+
if D == 4:
|
168 |
+
assert _is_tensor_eq(st, (result1)) or _is_tensor_eq(st, (result2))
|
169 |
+
else:
|
170 |
+
assert _is_tensor_eq(st, (result1))
|
171 |
+
|
172 |
+
# and a few very simple cases, with no contracted indices:
|
173 |
+
|
174 |
+
t = G(m0)
|
175 |
+
st = tfunc(t)
|
176 |
+
assert _is_tensor_eq(st, t)
|
177 |
+
|
178 |
+
t = -7*G(m0)
|
179 |
+
st = tfunc(t)
|
180 |
+
assert _is_tensor_eq(st, t)
|
181 |
+
|
182 |
+
t = 224*G(m0)*G(m1)*G(-m2)*G(m3)
|
183 |
+
st = tfunc(t)
|
184 |
+
assert _is_tensor_eq(st, t)
|
185 |
+
|
186 |
+
|
187 |
+
def test_kahane_algorithm():
|
188 |
+
# Wrap this function to convert to and from TIDS:
|
189 |
+
|
190 |
+
def tfunc(e):
|
191 |
+
return _simplify_single_line(e)
|
192 |
+
|
193 |
+
execute_gamma_simplify_tests_for_function(tfunc, D=4)
|
194 |
+
|
195 |
+
|
196 |
+
def test_kahane_simplify1():
|
197 |
+
i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15 = tensor_indices('i0:16', LorentzIndex)
|
198 |
+
mu, nu, rho, sigma = tensor_indices("mu, nu, rho, sigma", LorentzIndex)
|
199 |
+
D = 4
|
200 |
+
t = G(i0)*G(i1)
|
201 |
+
r = kahane_simplify(t)
|
202 |
+
assert r.equals(t)
|
203 |
+
|
204 |
+
t = G(i0)*G(i1)*G(-i0)
|
205 |
+
r = kahane_simplify(t)
|
206 |
+
assert r.equals(-2*G(i1))
|
207 |
+
t = G(i0)*G(i1)*G(-i0)
|
208 |
+
r = kahane_simplify(t)
|
209 |
+
assert r.equals(-2*G(i1))
|
210 |
+
|
211 |
+
t = G(i0)*G(i1)
|
212 |
+
r = kahane_simplify(t)
|
213 |
+
assert r.equals(t)
|
214 |
+
t = G(i0)*G(i1)
|
215 |
+
r = kahane_simplify(t)
|
216 |
+
assert r.equals(t)
|
217 |
+
t = G(i0)*G(-i0)
|
218 |
+
r = kahane_simplify(t)
|
219 |
+
assert r.equals(4*eye(4))
|
220 |
+
t = G(i0)*G(-i0)
|
221 |
+
r = kahane_simplify(t)
|
222 |
+
assert r.equals(4*eye(4))
|
223 |
+
t = G(i0)*G(-i0)
|
224 |
+
r = kahane_simplify(t)
|
225 |
+
assert r.equals(4*eye(4))
|
226 |
+
t = G(i0)*G(i1)*G(-i0)
|
227 |
+
r = kahane_simplify(t)
|
228 |
+
assert r.equals(-2*G(i1))
|
229 |
+
t = G(i0)*G(i1)*G(-i0)*G(-i1)
|
230 |
+
r = kahane_simplify(t)
|
231 |
+
assert r.equals((2*D - D**2)*eye(4))
|
232 |
+
t = G(i0)*G(i1)*G(-i0)*G(-i1)
|
233 |
+
r = kahane_simplify(t)
|
234 |
+
assert r.equals((2*D - D**2)*eye(4))
|
235 |
+
t = G(i0)*G(-i0)*G(i1)*G(-i1)
|
236 |
+
r = kahane_simplify(t)
|
237 |
+
assert r.equals(16*eye(4))
|
238 |
+
t = (G(mu)*G(nu)*G(-nu)*G(-mu))
|
239 |
+
r = kahane_simplify(t)
|
240 |
+
assert r.equals(D**2*eye(4))
|
241 |
+
t = (G(mu)*G(nu)*G(-nu)*G(-mu))
|
242 |
+
r = kahane_simplify(t)
|
243 |
+
assert r.equals(D**2*eye(4))
|
244 |
+
t = (G(mu)*G(nu)*G(-nu)*G(-mu))
|
245 |
+
r = kahane_simplify(t)
|
246 |
+
assert r.equals(D**2*eye(4))
|
247 |
+
t = (G(mu)*G(nu)*G(-rho)*G(-nu)*G(-mu)*G(rho))
|
248 |
+
r = kahane_simplify(t)
|
249 |
+
assert r.equals((4*D - 4*D**2 + D**3)*eye(4))
|
250 |
+
t = (G(-mu)*G(-nu)*G(-rho)*G(-sigma)*G(nu)*G(mu)*G(sigma)*G(rho))
|
251 |
+
r = kahane_simplify(t)
|
252 |
+
assert r.equals((-16*D + 24*D**2 - 8*D**3 + D**4)*eye(4))
|
253 |
+
t = (G(-mu)*G(nu)*G(-rho)*G(sigma)*G(rho)*G(-nu)*G(mu)*G(-sigma))
|
254 |
+
r = kahane_simplify(t)
|
255 |
+
assert r.equals((8*D - 12*D**2 + 6*D**3 - D**4)*eye(4))
|
256 |
+
|
257 |
+
# Expressions with free indices:
|
258 |
+
t = (G(mu)*G(nu)*G(rho)*G(sigma)*G(-mu))
|
259 |
+
r = kahane_simplify(t)
|
260 |
+
assert r.equals(-2*G(sigma)*G(rho)*G(nu))
|
261 |
+
t = (G(mu)*G(-mu)*G(rho)*G(sigma))
|
262 |
+
r = kahane_simplify(t)
|
263 |
+
assert r.equals(4*G(rho)*G(sigma))
|
264 |
+
t = (G(rho)*G(sigma)*G(mu)*G(-mu))
|
265 |
+
r = kahane_simplify(t)
|
266 |
+
assert r.equals(4*G(rho)*G(sigma))
|
267 |
+
|
268 |
+
def test_gamma_matrix_class():
|
269 |
+
i, j, k = tensor_indices('i,j,k', LorentzIndex)
|
270 |
+
|
271 |
+
# define another type of TensorHead to see if exprs are correctly handled:
|
272 |
+
A = TensorHead('A', [LorentzIndex])
|
273 |
+
|
274 |
+
t = A(k)*G(i)*G(-i)
|
275 |
+
ts = simplify_gamma_expression(t)
|
276 |
+
assert _is_tensor_eq(ts, Matrix([
|
277 |
+
[4, 0, 0, 0],
|
278 |
+
[0, 4, 0, 0],
|
279 |
+
[0, 0, 4, 0],
|
280 |
+
[0, 0, 0, 4]])*A(k))
|
281 |
+
|
282 |
+
t = G(i)*A(k)*G(j)
|
283 |
+
ts = simplify_gamma_expression(t)
|
284 |
+
assert _is_tensor_eq(ts, A(k)*G(i)*G(j))
|
285 |
+
|
286 |
+
execute_gamma_simplify_tests_for_function(simplify_gamma_expression, D=4)
|
287 |
+
|
288 |
+
|
289 |
+
def test_gamma_matrix_trace():
|
290 |
+
g = LorentzIndex.metric
|
291 |
+
|
292 |
+
m0, m1, m2, m3, m4, m5, m6 = tensor_indices('m0:7', LorentzIndex)
|
293 |
+
n0, n1, n2, n3, n4, n5 = tensor_indices('n0:6', LorentzIndex)
|
294 |
+
|
295 |
+
# working in D=4 dimensions
|
296 |
+
D = 4
|
297 |
+
|
298 |
+
# traces of odd number of gamma matrices are zero:
|
299 |
+
t = G(m0)
|
300 |
+
t1 = gamma_trace(t)
|
301 |
+
assert t1.equals(0)
|
302 |
+
|
303 |
+
t = G(m0)*G(m1)*G(m2)
|
304 |
+
t1 = gamma_trace(t)
|
305 |
+
assert t1.equals(0)
|
306 |
+
|
307 |
+
t = G(m0)*G(m1)*G(-m0)
|
308 |
+
t1 = gamma_trace(t)
|
309 |
+
assert t1.equals(0)
|
310 |
+
|
311 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)
|
312 |
+
t1 = gamma_trace(t)
|
313 |
+
assert t1.equals(0)
|
314 |
+
|
315 |
+
# traces without internal contractions:
|
316 |
+
t = G(m0)*G(m1)
|
317 |
+
t1 = gamma_trace(t)
|
318 |
+
assert _is_tensor_eq(t1, 4*g(m0, m1))
|
319 |
+
|
320 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)
|
321 |
+
t1 = gamma_trace(t)
|
322 |
+
t2 = -4*g(m0, m2)*g(m1, m3) + 4*g(m0, m1)*g(m2, m3) + 4*g(m0, m3)*g(m1, m2)
|
323 |
+
assert _is_tensor_eq(t1, t2)
|
324 |
+
|
325 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(m5)
|
326 |
+
t1 = gamma_trace(t)
|
327 |
+
t2 = t1*g(-m0, -m5)
|
328 |
+
t2 = t2.contract_metric(g)
|
329 |
+
assert _is_tensor_eq(t2, D*gamma_trace(G(m1)*G(m2)*G(m3)*G(m4)))
|
330 |
+
|
331 |
+
# traces of expressions with internal contractions:
|
332 |
+
t = G(m0)*G(-m0)
|
333 |
+
t1 = gamma_trace(t)
|
334 |
+
assert t1.equals(4*D)
|
335 |
+
|
336 |
+
t = G(m0)*G(m1)*G(-m0)*G(-m1)
|
337 |
+
t1 = gamma_trace(t)
|
338 |
+
assert t1.equals(8*D - 4*D**2)
|
339 |
+
|
340 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(-m0)
|
341 |
+
t1 = gamma_trace(t)
|
342 |
+
t2 = (-4*D)*g(m1, m3)*g(m2, m4) + (4*D)*g(m1, m2)*g(m3, m4) + \
|
343 |
+
(4*D)*g(m1, m4)*g(m2, m3)
|
344 |
+
assert _is_tensor_eq(t1, t2)
|
345 |
+
|
346 |
+
t = G(-m5)*G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(-m0)*G(m5)
|
347 |
+
t1 = gamma_trace(t)
|
348 |
+
t2 = (32*D + 4*(-D + 4)**2 - 64)*(g(m1, m2)*g(m3, m4) - \
|
349 |
+
g(m1, m3)*g(m2, m4) + g(m1, m4)*g(m2, m3))
|
350 |
+
assert _is_tensor_eq(t1, t2)
|
351 |
+
|
352 |
+
t = G(m0)*G(m1)*G(-m0)*G(m3)
|
353 |
+
t1 = gamma_trace(t)
|
354 |
+
assert t1.equals((-4*D + 8)*g(m1, m3))
|
355 |
+
|
356 |
+
# p, q = S1('p,q')
|
357 |
+
# ps = p(m0)*G(-m0)
|
358 |
+
# qs = q(m0)*G(-m0)
|
359 |
+
# t = ps*qs*ps*qs
|
360 |
+
# t1 = gamma_trace(t)
|
361 |
+
# assert t1 == 8*p(m0)*q(-m0)*p(m1)*q(-m1) - 4*p(m0)*p(-m0)*q(m1)*q(-m1)
|
362 |
+
|
363 |
+
t = G(m0)*G(m1)*G(m2)*G(m3)*G(m4)*G(m5)*G(-m0)*G(-m1)*G(-m2)*G(-m3)*G(-m4)*G(-m5)
|
364 |
+
t1 = gamma_trace(t)
|
365 |
+
assert t1.equals(-4*D**6 + 120*D**5 - 1040*D**4 + 3360*D**3 - 4480*D**2 + 2048*D)
|
366 |
+
|
367 |
+
t = G(m0)*G(m1)*G(n1)*G(m2)*G(n2)*G(m3)*G(m4)*G(-n2)*G(-n1)*G(-m0)*G(-m1)*G(-m2)*G(-m3)*G(-m4)
|
368 |
+
t1 = gamma_trace(t)
|
369 |
+
tresu = -7168*D + 16768*D**2 - 14400*D**3 + 5920*D**4 - 1232*D**5 + 120*D**6 - 4*D**7
|
370 |
+
assert t1.equals(tresu)
|
371 |
+
|
372 |
+
# checked with Mathematica
|
373 |
+
# In[1]:= <<Tracer.m
|
374 |
+
# In[2]:= Spur[l];
|
375 |
+
# In[3]:= GammaTrace[l, {m0},{m1},{n1},{m2},{n2},{m3},{m4},{n3},{n4},{m0},{m1},{m2},{m3},{m4}]
|
376 |
+
t = G(m0)*G(m1)*G(n1)*G(m2)*G(n2)*G(m3)*G(m4)*G(n3)*G(n4)*G(-m0)*G(-m1)*G(-m2)*G(-m3)*G(-m4)
|
377 |
+
t1 = gamma_trace(t)
|
378 |
+
# t1 = t1.expand_coeff()
|
379 |
+
c1 = -4*D**5 + 120*D**4 - 1200*D**3 + 5280*D**2 - 10560*D + 7808
|
380 |
+
c2 = -4*D**5 + 88*D**4 - 560*D**3 + 1440*D**2 - 1600*D + 640
|
381 |
+
assert _is_tensor_eq(t1, c1*g(n1, n4)*g(n2, n3) + c2*g(n1, n2)*g(n3, n4) + \
|
382 |
+
(-c1)*g(n1, n3)*g(n2, n4))
|
383 |
+
|
384 |
+
p, q = tensor_heads('p,q', [LorentzIndex])
|
385 |
+
ps = p(m0)*G(-m0)
|
386 |
+
qs = q(m0)*G(-m0)
|
387 |
+
p2 = p(m0)*p(-m0)
|
388 |
+
q2 = q(m0)*q(-m0)
|
389 |
+
pq = p(m0)*q(-m0)
|
390 |
+
t = ps*qs*ps*qs
|
391 |
+
r = gamma_trace(t)
|
392 |
+
assert _is_tensor_eq(r, 8*pq*pq - 4*p2*q2)
|
393 |
+
t = ps*qs*ps*qs*ps*qs
|
394 |
+
r = gamma_trace(t)
|
395 |
+
assert _is_tensor_eq(r, -12*p2*pq*q2 + 16*pq*pq*pq)
|
396 |
+
t = ps*qs*ps*qs*ps*qs*ps*qs
|
397 |
+
r = gamma_trace(t)
|
398 |
+
assert _is_tensor_eq(r, -32*pq*pq*p2*q2 + 32*pq*pq*pq*pq + 4*p2*p2*q2*q2)
|
399 |
+
|
400 |
+
t = 4*p(m1)*p(m0)*p(-m0)*q(-m1)*q(m2)*q(-m2)
|
401 |
+
assert _is_tensor_eq(gamma_trace(t), t)
|
402 |
+
t = ps*ps*ps*ps*ps*ps*ps*ps
|
403 |
+
r = gamma_trace(t)
|
404 |
+
assert r.equals(4*p2*p2*p2*p2)
|
405 |
+
|
406 |
+
|
407 |
+
def test_bug_13636():
|
408 |
+
"""Test issue 13636 regarding handling traces of sums of products
|
409 |
+
of GammaMatrix mixed with other factors."""
|
410 |
+
pi, ki, pf = tensor_heads("pi, ki, pf", [LorentzIndex])
|
411 |
+
i0, i1, i2, i3, i4 = tensor_indices("i0:5", LorentzIndex)
|
412 |
+
x = Symbol("x")
|
413 |
+
pis = pi(i2) * G(-i2)
|
414 |
+
kis = ki(i3) * G(-i3)
|
415 |
+
pfs = pf(i4) * G(-i4)
|
416 |
+
|
417 |
+
a = pfs * G(i0) * kis * G(i1) * pis * G(-i1) * kis * G(-i0)
|
418 |
+
b = pfs * G(i0) * kis * G(i1) * pis * x * G(-i0) * pi(-i1)
|
419 |
+
ta = gamma_trace(a)
|
420 |
+
tb = gamma_trace(b)
|
421 |
+
t_a_plus_b = gamma_trace(a + b)
|
422 |
+
assert ta == 4 * (
|
423 |
+
-4 * ki(i0) * ki(-i0) * pf(i1) * pi(-i1)
|
424 |
+
+ 8 * ki(i0) * ki(i1) * pf(-i0) * pi(-i1)
|
425 |
+
)
|
426 |
+
assert tb == -8 * x * ki(i0) * pf(-i0) * pi(i1) * pi(-i1)
|
427 |
+
assert t_a_plus_b == ta + tb
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__init__.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
__all__ = [
|
2 |
+
'TWave',
|
3 |
+
|
4 |
+
'RayTransferMatrix', 'FreeSpace', 'FlatRefraction', 'CurvedRefraction',
|
5 |
+
'FlatMirror', 'CurvedMirror', 'ThinLens', 'GeometricRay', 'BeamParameter',
|
6 |
+
'waist2rayleigh', 'rayleigh2waist', 'geometric_conj_ab',
|
7 |
+
'geometric_conj_af', 'geometric_conj_bf', 'gaussian_conj',
|
8 |
+
'conjugate_gauss_beams',
|
9 |
+
|
10 |
+
'Medium',
|
11 |
+
|
12 |
+
'refraction_angle', 'deviation', 'fresnel_coefficients', 'brewster_angle',
|
13 |
+
'critical_angle', 'lens_makers_formula', 'mirror_formula', 'lens_formula',
|
14 |
+
'hyperfocal_distance', 'transverse_magnification',
|
15 |
+
|
16 |
+
'jones_vector', 'stokes_vector', 'jones_2_stokes', 'linear_polarizer',
|
17 |
+
'phase_retarder', 'half_wave_retarder', 'quarter_wave_retarder',
|
18 |
+
'transmissive_filter', 'reflective_filter', 'mueller_matrix',
|
19 |
+
'polarizing_beam_splitter',
|
20 |
+
]
|
21 |
+
from .waves import TWave
|
22 |
+
|
23 |
+
from .gaussopt import (RayTransferMatrix, FreeSpace, FlatRefraction,
|
24 |
+
CurvedRefraction, FlatMirror, CurvedMirror, ThinLens, GeometricRay,
|
25 |
+
BeamParameter, waist2rayleigh, rayleigh2waist, geometric_conj_ab,
|
26 |
+
geometric_conj_af, geometric_conj_bf, gaussian_conj,
|
27 |
+
conjugate_gauss_beams)
|
28 |
+
|
29 |
+
from .medium import Medium
|
30 |
+
|
31 |
+
from .utils import (refraction_angle, deviation, fresnel_coefficients,
|
32 |
+
brewster_angle, critical_angle, lens_makers_formula, mirror_formula,
|
33 |
+
lens_formula, hyperfocal_distance, transverse_magnification)
|
34 |
+
|
35 |
+
from .polarization import (jones_vector, stokes_vector, jones_2_stokes,
|
36 |
+
linear_polarizer, phase_retarder, half_wave_retarder,
|
37 |
+
quarter_wave_retarder, transmissive_filter, reflective_filter,
|
38 |
+
mueller_matrix, polarizing_beam_splitter)
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.52 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/gaussopt.cpython-310.pyc
ADDED
Binary file (21.9 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/medium.cpython-310.pyc
ADDED
Binary file (7.53 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/polarization.cpython-310.pyc
ADDED
Binary file (22.3 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/utils.cpython-310.pyc
ADDED
Binary file (17.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/__pycache__/waves.cpython-310.pyc
ADDED
Binary file (10.6 kB). View file
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Gaussian optics.
|
3 |
+
|
4 |
+
The module implements:
|
5 |
+
|
6 |
+
- Ray transfer matrices for geometrical and gaussian optics.
|
7 |
+
|
8 |
+
See RayTransferMatrix, GeometricRay and BeamParameter
|
9 |
+
|
10 |
+
- Conjugation relations for geometrical and gaussian optics.
|
11 |
+
|
12 |
+
See geometric_conj*, gauss_conj and conjugate_gauss_beams
|
13 |
+
|
14 |
+
The conventions for the distances are as follows:
|
15 |
+
|
16 |
+
focal distance
|
17 |
+
positive for convergent lenses
|
18 |
+
object distance
|
19 |
+
positive for real objects
|
20 |
+
image distance
|
21 |
+
positive for real images
|
22 |
+
"""
|
23 |
+
|
24 |
+
__all__ = [
|
25 |
+
'RayTransferMatrix',
|
26 |
+
'FreeSpace',
|
27 |
+
'FlatRefraction',
|
28 |
+
'CurvedRefraction',
|
29 |
+
'FlatMirror',
|
30 |
+
'CurvedMirror',
|
31 |
+
'ThinLens',
|
32 |
+
'GeometricRay',
|
33 |
+
'BeamParameter',
|
34 |
+
'waist2rayleigh',
|
35 |
+
'rayleigh2waist',
|
36 |
+
'geometric_conj_ab',
|
37 |
+
'geometric_conj_af',
|
38 |
+
'geometric_conj_bf',
|
39 |
+
'gaussian_conj',
|
40 |
+
'conjugate_gauss_beams',
|
41 |
+
]
|
42 |
+
|
43 |
+
|
44 |
+
from sympy.core.expr import Expr
|
45 |
+
from sympy.core.numbers import (I, pi)
|
46 |
+
from sympy.core.sympify import sympify
|
47 |
+
from sympy.functions.elementary.complexes import (im, re)
|
48 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
49 |
+
from sympy.functions.elementary.trigonometric import atan2
|
50 |
+
from sympy.matrices.dense import Matrix, MutableDenseMatrix
|
51 |
+
from sympy.polys.rationaltools import together
|
52 |
+
from sympy.utilities.misc import filldedent
|
53 |
+
|
54 |
+
###
|
55 |
+
# A, B, C, D matrices
|
56 |
+
###
|
57 |
+
|
58 |
+
|
59 |
+
class RayTransferMatrix(MutableDenseMatrix):
|
60 |
+
"""
|
61 |
+
Base class for a Ray Transfer Matrix.
|
62 |
+
|
63 |
+
It should be used if there is not already a more specific subclass mentioned
|
64 |
+
in See Also.
|
65 |
+
|
66 |
+
Parameters
|
67 |
+
==========
|
68 |
+
|
69 |
+
parameters :
|
70 |
+
A, B, C and D or 2x2 matrix (Matrix(2, 2, [A, B, C, D]))
|
71 |
+
|
72 |
+
Examples
|
73 |
+
========
|
74 |
+
|
75 |
+
>>> from sympy.physics.optics import RayTransferMatrix, ThinLens
|
76 |
+
>>> from sympy import Symbol, Matrix
|
77 |
+
|
78 |
+
>>> mat = RayTransferMatrix(1, 2, 3, 4)
|
79 |
+
>>> mat
|
80 |
+
Matrix([
|
81 |
+
[1, 2],
|
82 |
+
[3, 4]])
|
83 |
+
|
84 |
+
>>> RayTransferMatrix(Matrix([[1, 2], [3, 4]]))
|
85 |
+
Matrix([
|
86 |
+
[1, 2],
|
87 |
+
[3, 4]])
|
88 |
+
|
89 |
+
>>> mat.A
|
90 |
+
1
|
91 |
+
|
92 |
+
>>> f = Symbol('f')
|
93 |
+
>>> lens = ThinLens(f)
|
94 |
+
>>> lens
|
95 |
+
Matrix([
|
96 |
+
[ 1, 0],
|
97 |
+
[-1/f, 1]])
|
98 |
+
|
99 |
+
>>> lens.C
|
100 |
+
-1/f
|
101 |
+
|
102 |
+
See Also
|
103 |
+
========
|
104 |
+
|
105 |
+
GeometricRay, BeamParameter,
|
106 |
+
FreeSpace, FlatRefraction, CurvedRefraction,
|
107 |
+
FlatMirror, CurvedMirror, ThinLens
|
108 |
+
|
109 |
+
References
|
110 |
+
==========
|
111 |
+
|
112 |
+
.. [1] https://en.wikipedia.org/wiki/Ray_transfer_matrix_analysis
|
113 |
+
"""
|
114 |
+
|
115 |
+
def __new__(cls, *args):
|
116 |
+
|
117 |
+
if len(args) == 4:
|
118 |
+
temp = ((args[0], args[1]), (args[2], args[3]))
|
119 |
+
elif len(args) == 1 \
|
120 |
+
and isinstance(args[0], Matrix) \
|
121 |
+
and args[0].shape == (2, 2):
|
122 |
+
temp = args[0]
|
123 |
+
else:
|
124 |
+
raise ValueError(filldedent('''
|
125 |
+
Expecting 2x2 Matrix or the 4 elements of
|
126 |
+
the Matrix but got %s''' % str(args)))
|
127 |
+
return Matrix.__new__(cls, temp)
|
128 |
+
|
129 |
+
def __mul__(self, other):
|
130 |
+
if isinstance(other, RayTransferMatrix):
|
131 |
+
return RayTransferMatrix(Matrix.__mul__(self, other))
|
132 |
+
elif isinstance(other, GeometricRay):
|
133 |
+
return GeometricRay(Matrix.__mul__(self, other))
|
134 |
+
elif isinstance(other, BeamParameter):
|
135 |
+
temp = self*Matrix(((other.q,), (1,)))
|
136 |
+
q = (temp[0]/temp[1]).expand(complex=True)
|
137 |
+
return BeamParameter(other.wavelen,
|
138 |
+
together(re(q)),
|
139 |
+
z_r=together(im(q)))
|
140 |
+
else:
|
141 |
+
return Matrix.__mul__(self, other)
|
142 |
+
|
143 |
+
@property
|
144 |
+
def A(self):
|
145 |
+
"""
|
146 |
+
The A parameter of the Matrix.
|
147 |
+
|
148 |
+
Examples
|
149 |
+
========
|
150 |
+
|
151 |
+
>>> from sympy.physics.optics import RayTransferMatrix
|
152 |
+
>>> mat = RayTransferMatrix(1, 2, 3, 4)
|
153 |
+
>>> mat.A
|
154 |
+
1
|
155 |
+
"""
|
156 |
+
return self[0, 0]
|
157 |
+
|
158 |
+
@property
|
159 |
+
def B(self):
|
160 |
+
"""
|
161 |
+
The B parameter of the Matrix.
|
162 |
+
|
163 |
+
Examples
|
164 |
+
========
|
165 |
+
|
166 |
+
>>> from sympy.physics.optics import RayTransferMatrix
|
167 |
+
>>> mat = RayTransferMatrix(1, 2, 3, 4)
|
168 |
+
>>> mat.B
|
169 |
+
2
|
170 |
+
"""
|
171 |
+
return self[0, 1]
|
172 |
+
|
173 |
+
@property
|
174 |
+
def C(self):
|
175 |
+
"""
|
176 |
+
The C parameter of the Matrix.
|
177 |
+
|
178 |
+
Examples
|
179 |
+
========
|
180 |
+
|
181 |
+
>>> from sympy.physics.optics import RayTransferMatrix
|
182 |
+
>>> mat = RayTransferMatrix(1, 2, 3, 4)
|
183 |
+
>>> mat.C
|
184 |
+
3
|
185 |
+
"""
|
186 |
+
return self[1, 0]
|
187 |
+
|
188 |
+
@property
|
189 |
+
def D(self):
|
190 |
+
"""
|
191 |
+
The D parameter of the Matrix.
|
192 |
+
|
193 |
+
Examples
|
194 |
+
========
|
195 |
+
|
196 |
+
>>> from sympy.physics.optics import RayTransferMatrix
|
197 |
+
>>> mat = RayTransferMatrix(1, 2, 3, 4)
|
198 |
+
>>> mat.D
|
199 |
+
4
|
200 |
+
"""
|
201 |
+
return self[1, 1]
|
202 |
+
|
203 |
+
|
204 |
+
class FreeSpace(RayTransferMatrix):
|
205 |
+
"""
|
206 |
+
Ray Transfer Matrix for free space.
|
207 |
+
|
208 |
+
Parameters
|
209 |
+
==========
|
210 |
+
|
211 |
+
distance
|
212 |
+
|
213 |
+
See Also
|
214 |
+
========
|
215 |
+
|
216 |
+
RayTransferMatrix
|
217 |
+
|
218 |
+
Examples
|
219 |
+
========
|
220 |
+
|
221 |
+
>>> from sympy.physics.optics import FreeSpace
|
222 |
+
>>> from sympy import symbols
|
223 |
+
>>> d = symbols('d')
|
224 |
+
>>> FreeSpace(d)
|
225 |
+
Matrix([
|
226 |
+
[1, d],
|
227 |
+
[0, 1]])
|
228 |
+
"""
|
229 |
+
def __new__(cls, d):
|
230 |
+
return RayTransferMatrix.__new__(cls, 1, d, 0, 1)
|
231 |
+
|
232 |
+
|
233 |
+
class FlatRefraction(RayTransferMatrix):
|
234 |
+
"""
|
235 |
+
Ray Transfer Matrix for refraction.
|
236 |
+
|
237 |
+
Parameters
|
238 |
+
==========
|
239 |
+
|
240 |
+
n1 :
|
241 |
+
Refractive index of one medium.
|
242 |
+
n2 :
|
243 |
+
Refractive index of other medium.
|
244 |
+
|
245 |
+
See Also
|
246 |
+
========
|
247 |
+
|
248 |
+
RayTransferMatrix
|
249 |
+
|
250 |
+
Examples
|
251 |
+
========
|
252 |
+
|
253 |
+
>>> from sympy.physics.optics import FlatRefraction
|
254 |
+
>>> from sympy import symbols
|
255 |
+
>>> n1, n2 = symbols('n1 n2')
|
256 |
+
>>> FlatRefraction(n1, n2)
|
257 |
+
Matrix([
|
258 |
+
[1, 0],
|
259 |
+
[0, n1/n2]])
|
260 |
+
"""
|
261 |
+
def __new__(cls, n1, n2):
|
262 |
+
n1, n2 = map(sympify, (n1, n2))
|
263 |
+
return RayTransferMatrix.__new__(cls, 1, 0, 0, n1/n2)
|
264 |
+
|
265 |
+
|
266 |
+
class CurvedRefraction(RayTransferMatrix):
|
267 |
+
"""
|
268 |
+
Ray Transfer Matrix for refraction on curved interface.
|
269 |
+
|
270 |
+
Parameters
|
271 |
+
==========
|
272 |
+
|
273 |
+
R :
|
274 |
+
Radius of curvature (positive for concave).
|
275 |
+
n1 :
|
276 |
+
Refractive index of one medium.
|
277 |
+
n2 :
|
278 |
+
Refractive index of other medium.
|
279 |
+
|
280 |
+
See Also
|
281 |
+
========
|
282 |
+
|
283 |
+
RayTransferMatrix
|
284 |
+
|
285 |
+
Examples
|
286 |
+
========
|
287 |
+
|
288 |
+
>>> from sympy.physics.optics import CurvedRefraction
|
289 |
+
>>> from sympy import symbols
|
290 |
+
>>> R, n1, n2 = symbols('R n1 n2')
|
291 |
+
>>> CurvedRefraction(R, n1, n2)
|
292 |
+
Matrix([
|
293 |
+
[ 1, 0],
|
294 |
+
[(n1 - n2)/(R*n2), n1/n2]])
|
295 |
+
"""
|
296 |
+
def __new__(cls, R, n1, n2):
|
297 |
+
R, n1, n2 = map(sympify, (R, n1, n2))
|
298 |
+
return RayTransferMatrix.__new__(cls, 1, 0, (n1 - n2)/R/n2, n1/n2)
|
299 |
+
|
300 |
+
|
301 |
+
class FlatMirror(RayTransferMatrix):
|
302 |
+
"""
|
303 |
+
Ray Transfer Matrix for reflection.
|
304 |
+
|
305 |
+
See Also
|
306 |
+
========
|
307 |
+
|
308 |
+
RayTransferMatrix
|
309 |
+
|
310 |
+
Examples
|
311 |
+
========
|
312 |
+
|
313 |
+
>>> from sympy.physics.optics import FlatMirror
|
314 |
+
>>> FlatMirror()
|
315 |
+
Matrix([
|
316 |
+
[1, 0],
|
317 |
+
[0, 1]])
|
318 |
+
"""
|
319 |
+
def __new__(cls):
|
320 |
+
return RayTransferMatrix.__new__(cls, 1, 0, 0, 1)
|
321 |
+
|
322 |
+
|
323 |
+
class CurvedMirror(RayTransferMatrix):
|
324 |
+
"""
|
325 |
+
Ray Transfer Matrix for reflection from curved surface.
|
326 |
+
|
327 |
+
Parameters
|
328 |
+
==========
|
329 |
+
|
330 |
+
R : radius of curvature (positive for concave)
|
331 |
+
|
332 |
+
See Also
|
333 |
+
========
|
334 |
+
|
335 |
+
RayTransferMatrix
|
336 |
+
|
337 |
+
Examples
|
338 |
+
========
|
339 |
+
|
340 |
+
>>> from sympy.physics.optics import CurvedMirror
|
341 |
+
>>> from sympy import symbols
|
342 |
+
>>> R = symbols('R')
|
343 |
+
>>> CurvedMirror(R)
|
344 |
+
Matrix([
|
345 |
+
[ 1, 0],
|
346 |
+
[-2/R, 1]])
|
347 |
+
"""
|
348 |
+
def __new__(cls, R):
|
349 |
+
R = sympify(R)
|
350 |
+
return RayTransferMatrix.__new__(cls, 1, 0, -2/R, 1)
|
351 |
+
|
352 |
+
|
353 |
+
class ThinLens(RayTransferMatrix):
|
354 |
+
"""
|
355 |
+
Ray Transfer Matrix for a thin lens.
|
356 |
+
|
357 |
+
Parameters
|
358 |
+
==========
|
359 |
+
|
360 |
+
f :
|
361 |
+
The focal distance.
|
362 |
+
|
363 |
+
See Also
|
364 |
+
========
|
365 |
+
|
366 |
+
RayTransferMatrix
|
367 |
+
|
368 |
+
Examples
|
369 |
+
========
|
370 |
+
|
371 |
+
>>> from sympy.physics.optics import ThinLens
|
372 |
+
>>> from sympy import symbols
|
373 |
+
>>> f = symbols('f')
|
374 |
+
>>> ThinLens(f)
|
375 |
+
Matrix([
|
376 |
+
[ 1, 0],
|
377 |
+
[-1/f, 1]])
|
378 |
+
"""
|
379 |
+
def __new__(cls, f):
|
380 |
+
f = sympify(f)
|
381 |
+
return RayTransferMatrix.__new__(cls, 1, 0, -1/f, 1)
|
382 |
+
|
383 |
+
|
384 |
+
###
|
385 |
+
# Representation for geometric ray
|
386 |
+
###
|
387 |
+
|
388 |
+
class GeometricRay(MutableDenseMatrix):
|
389 |
+
"""
|
390 |
+
Representation for a geometric ray in the Ray Transfer Matrix formalism.
|
391 |
+
|
392 |
+
Parameters
|
393 |
+
==========
|
394 |
+
|
395 |
+
h : height, and
|
396 |
+
angle : angle, or
|
397 |
+
matrix : a 2x1 matrix (Matrix(2, 1, [height, angle]))
|
398 |
+
|
399 |
+
Examples
|
400 |
+
========
|
401 |
+
|
402 |
+
>>> from sympy.physics.optics import GeometricRay, FreeSpace
|
403 |
+
>>> from sympy import symbols, Matrix
|
404 |
+
>>> d, h, angle = symbols('d, h, angle')
|
405 |
+
|
406 |
+
>>> GeometricRay(h, angle)
|
407 |
+
Matrix([
|
408 |
+
[ h],
|
409 |
+
[angle]])
|
410 |
+
|
411 |
+
>>> FreeSpace(d)*GeometricRay(h, angle)
|
412 |
+
Matrix([
|
413 |
+
[angle*d + h],
|
414 |
+
[ angle]])
|
415 |
+
|
416 |
+
>>> GeometricRay( Matrix( ((h,), (angle,)) ) )
|
417 |
+
Matrix([
|
418 |
+
[ h],
|
419 |
+
[angle]])
|
420 |
+
|
421 |
+
See Also
|
422 |
+
========
|
423 |
+
|
424 |
+
RayTransferMatrix
|
425 |
+
|
426 |
+
"""
|
427 |
+
|
428 |
+
def __new__(cls, *args):
|
429 |
+
if len(args) == 1 and isinstance(args[0], Matrix) \
|
430 |
+
and args[0].shape == (2, 1):
|
431 |
+
temp = args[0]
|
432 |
+
elif len(args) == 2:
|
433 |
+
temp = ((args[0],), (args[1],))
|
434 |
+
else:
|
435 |
+
raise ValueError(filldedent('''
|
436 |
+
Expecting 2x1 Matrix or the 2 elements of
|
437 |
+
the Matrix but got %s''' % str(args)))
|
438 |
+
return Matrix.__new__(cls, temp)
|
439 |
+
|
440 |
+
@property
|
441 |
+
def height(self):
|
442 |
+
"""
|
443 |
+
The distance from the optical axis.
|
444 |
+
|
445 |
+
Examples
|
446 |
+
========
|
447 |
+
|
448 |
+
>>> from sympy.physics.optics import GeometricRay
|
449 |
+
>>> from sympy import symbols
|
450 |
+
>>> h, angle = symbols('h, angle')
|
451 |
+
>>> gRay = GeometricRay(h, angle)
|
452 |
+
>>> gRay.height
|
453 |
+
h
|
454 |
+
"""
|
455 |
+
return self[0]
|
456 |
+
|
457 |
+
@property
|
458 |
+
def angle(self):
|
459 |
+
"""
|
460 |
+
The angle with the optical axis.
|
461 |
+
|
462 |
+
Examples
|
463 |
+
========
|
464 |
+
|
465 |
+
>>> from sympy.physics.optics import GeometricRay
|
466 |
+
>>> from sympy import symbols
|
467 |
+
>>> h, angle = symbols('h, angle')
|
468 |
+
>>> gRay = GeometricRay(h, angle)
|
469 |
+
>>> gRay.angle
|
470 |
+
angle
|
471 |
+
"""
|
472 |
+
return self[1]
|
473 |
+
|
474 |
+
|
475 |
+
###
|
476 |
+
# Representation for gauss beam
|
477 |
+
###
|
478 |
+
|
479 |
+
class BeamParameter(Expr):
|
480 |
+
"""
|
481 |
+
Representation for a gaussian ray in the Ray Transfer Matrix formalism.
|
482 |
+
|
483 |
+
Parameters
|
484 |
+
==========
|
485 |
+
|
486 |
+
wavelen : the wavelength,
|
487 |
+
z : the distance to waist, and
|
488 |
+
w : the waist, or
|
489 |
+
z_r : the rayleigh range.
|
490 |
+
n : the refractive index of medium.
|
491 |
+
|
492 |
+
Examples
|
493 |
+
========
|
494 |
+
|
495 |
+
>>> from sympy.physics.optics import BeamParameter
|
496 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
497 |
+
>>> p.q
|
498 |
+
1 + 1.88679245283019*I*pi
|
499 |
+
|
500 |
+
>>> p.q.n()
|
501 |
+
1.0 + 5.92753330865999*I
|
502 |
+
>>> p.w_0.n()
|
503 |
+
0.00100000000000000
|
504 |
+
>>> p.z_r.n()
|
505 |
+
5.92753330865999
|
506 |
+
|
507 |
+
>>> from sympy.physics.optics import FreeSpace
|
508 |
+
>>> fs = FreeSpace(10)
|
509 |
+
>>> p1 = fs*p
|
510 |
+
>>> p.w.n()
|
511 |
+
0.00101413072159615
|
512 |
+
>>> p1.w.n()
|
513 |
+
0.00210803120913829
|
514 |
+
|
515 |
+
See Also
|
516 |
+
========
|
517 |
+
|
518 |
+
RayTransferMatrix
|
519 |
+
|
520 |
+
References
|
521 |
+
==========
|
522 |
+
|
523 |
+
.. [1] https://en.wikipedia.org/wiki/Complex_beam_parameter
|
524 |
+
.. [2] https://en.wikipedia.org/wiki/Gaussian_beam
|
525 |
+
"""
|
526 |
+
#TODO A class Complex may be implemented. The BeamParameter may
|
527 |
+
# subclass it. See:
|
528 |
+
# https://groups.google.com/d/topic/sympy/7XkU07NRBEs/discussion
|
529 |
+
|
530 |
+
def __new__(cls, wavelen, z, z_r=None, w=None, n=1):
|
531 |
+
wavelen = sympify(wavelen)
|
532 |
+
z = sympify(z)
|
533 |
+
n = sympify(n)
|
534 |
+
|
535 |
+
if z_r is not None and w is None:
|
536 |
+
z_r = sympify(z_r)
|
537 |
+
elif w is not None and z_r is None:
|
538 |
+
z_r = waist2rayleigh(sympify(w), wavelen, n)
|
539 |
+
elif z_r is None and w is None:
|
540 |
+
raise ValueError('Must specify one of w and z_r.')
|
541 |
+
|
542 |
+
return Expr.__new__(cls, wavelen, z, z_r, n)
|
543 |
+
|
544 |
+
@property
|
545 |
+
def wavelen(self):
|
546 |
+
return self.args[0]
|
547 |
+
|
548 |
+
@property
|
549 |
+
def z(self):
|
550 |
+
return self.args[1]
|
551 |
+
|
552 |
+
@property
|
553 |
+
def z_r(self):
|
554 |
+
return self.args[2]
|
555 |
+
|
556 |
+
@property
|
557 |
+
def n(self):
|
558 |
+
return self.args[3]
|
559 |
+
|
560 |
+
@property
|
561 |
+
def q(self):
|
562 |
+
"""
|
563 |
+
The complex parameter representing the beam.
|
564 |
+
|
565 |
+
Examples
|
566 |
+
========
|
567 |
+
|
568 |
+
>>> from sympy.physics.optics import BeamParameter
|
569 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
570 |
+
>>> p.q
|
571 |
+
1 + 1.88679245283019*I*pi
|
572 |
+
"""
|
573 |
+
return self.z + I*self.z_r
|
574 |
+
|
575 |
+
@property
|
576 |
+
def radius(self):
|
577 |
+
"""
|
578 |
+
The radius of curvature of the phase front.
|
579 |
+
|
580 |
+
Examples
|
581 |
+
========
|
582 |
+
|
583 |
+
>>> from sympy.physics.optics import BeamParameter
|
584 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
585 |
+
>>> p.radius
|
586 |
+
1 + 3.55998576005696*pi**2
|
587 |
+
"""
|
588 |
+
return self.z*(1 + (self.z_r/self.z)**2)
|
589 |
+
|
590 |
+
@property
|
591 |
+
def w(self):
|
592 |
+
"""
|
593 |
+
The radius of the beam w(z), at any position z along the beam.
|
594 |
+
The beam radius at `1/e^2` intensity (axial value).
|
595 |
+
|
596 |
+
See Also
|
597 |
+
========
|
598 |
+
|
599 |
+
w_0 :
|
600 |
+
The minimal radius of beam.
|
601 |
+
|
602 |
+
Examples
|
603 |
+
========
|
604 |
+
|
605 |
+
>>> from sympy.physics.optics import BeamParameter
|
606 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
607 |
+
>>> p.w
|
608 |
+
0.001*sqrt(0.2809/pi**2 + 1)
|
609 |
+
"""
|
610 |
+
return self.w_0*sqrt(1 + (self.z/self.z_r)**2)
|
611 |
+
|
612 |
+
@property
|
613 |
+
def w_0(self):
|
614 |
+
"""
|
615 |
+
The minimal radius of beam at `1/e^2` intensity (peak value).
|
616 |
+
|
617 |
+
See Also
|
618 |
+
========
|
619 |
+
|
620 |
+
w : the beam radius at `1/e^2` intensity (axial value).
|
621 |
+
|
622 |
+
Examples
|
623 |
+
========
|
624 |
+
|
625 |
+
>>> from sympy.physics.optics import BeamParameter
|
626 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
627 |
+
>>> p.w_0
|
628 |
+
0.00100000000000000
|
629 |
+
"""
|
630 |
+
return sqrt(self.z_r/(pi*self.n)*self.wavelen)
|
631 |
+
|
632 |
+
@property
|
633 |
+
def divergence(self):
|
634 |
+
"""
|
635 |
+
Half of the total angular spread.
|
636 |
+
|
637 |
+
Examples
|
638 |
+
========
|
639 |
+
|
640 |
+
>>> from sympy.physics.optics import BeamParameter
|
641 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
642 |
+
>>> p.divergence
|
643 |
+
0.00053/pi
|
644 |
+
"""
|
645 |
+
return self.wavelen/pi/self.w_0
|
646 |
+
|
647 |
+
@property
|
648 |
+
def gouy(self):
|
649 |
+
"""
|
650 |
+
The Gouy phase.
|
651 |
+
|
652 |
+
Examples
|
653 |
+
========
|
654 |
+
|
655 |
+
>>> from sympy.physics.optics import BeamParameter
|
656 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
657 |
+
>>> p.gouy
|
658 |
+
atan(0.53/pi)
|
659 |
+
"""
|
660 |
+
return atan2(self.z, self.z_r)
|
661 |
+
|
662 |
+
@property
|
663 |
+
def waist_approximation_limit(self):
|
664 |
+
"""
|
665 |
+
The minimal waist for which the gauss beam approximation is valid.
|
666 |
+
|
667 |
+
Explanation
|
668 |
+
===========
|
669 |
+
|
670 |
+
The gauss beam is a solution to the paraxial equation. For curvatures
|
671 |
+
that are too great it is not a valid approximation.
|
672 |
+
|
673 |
+
Examples
|
674 |
+
========
|
675 |
+
|
676 |
+
>>> from sympy.physics.optics import BeamParameter
|
677 |
+
>>> p = BeamParameter(530e-9, 1, w=1e-3)
|
678 |
+
>>> p.waist_approximation_limit
|
679 |
+
1.06e-6/pi
|
680 |
+
"""
|
681 |
+
return 2*self.wavelen/pi
|
682 |
+
|
683 |
+
|
684 |
+
###
|
685 |
+
# Utilities
|
686 |
+
###
|
687 |
+
|
688 |
+
def waist2rayleigh(w, wavelen, n=1):
|
689 |
+
"""
|
690 |
+
Calculate the rayleigh range from the waist of a gaussian beam.
|
691 |
+
|
692 |
+
See Also
|
693 |
+
========
|
694 |
+
|
695 |
+
rayleigh2waist, BeamParameter
|
696 |
+
|
697 |
+
Examples
|
698 |
+
========
|
699 |
+
|
700 |
+
>>> from sympy.physics.optics import waist2rayleigh
|
701 |
+
>>> from sympy import symbols
|
702 |
+
>>> w, wavelen = symbols('w wavelen')
|
703 |
+
>>> waist2rayleigh(w, wavelen)
|
704 |
+
pi*w**2/wavelen
|
705 |
+
"""
|
706 |
+
w, wavelen = map(sympify, (w, wavelen))
|
707 |
+
return w**2*n*pi/wavelen
|
708 |
+
|
709 |
+
|
710 |
+
def rayleigh2waist(z_r, wavelen):
|
711 |
+
"""Calculate the waist from the rayleigh range of a gaussian beam.
|
712 |
+
|
713 |
+
See Also
|
714 |
+
========
|
715 |
+
|
716 |
+
waist2rayleigh, BeamParameter
|
717 |
+
|
718 |
+
Examples
|
719 |
+
========
|
720 |
+
|
721 |
+
>>> from sympy.physics.optics import rayleigh2waist
|
722 |
+
>>> from sympy import symbols
|
723 |
+
>>> z_r, wavelen = symbols('z_r wavelen')
|
724 |
+
>>> rayleigh2waist(z_r, wavelen)
|
725 |
+
sqrt(wavelen*z_r)/sqrt(pi)
|
726 |
+
"""
|
727 |
+
z_r, wavelen = map(sympify, (z_r, wavelen))
|
728 |
+
return sqrt(z_r/pi*wavelen)
|
729 |
+
|
730 |
+
|
731 |
+
def geometric_conj_ab(a, b):
|
732 |
+
"""
|
733 |
+
Conjugation relation for geometrical beams under paraxial conditions.
|
734 |
+
|
735 |
+
Explanation
|
736 |
+
===========
|
737 |
+
|
738 |
+
Takes the distances to the optical element and returns the needed
|
739 |
+
focal distance.
|
740 |
+
|
741 |
+
See Also
|
742 |
+
========
|
743 |
+
|
744 |
+
geometric_conj_af, geometric_conj_bf
|
745 |
+
|
746 |
+
Examples
|
747 |
+
========
|
748 |
+
|
749 |
+
>>> from sympy.physics.optics import geometric_conj_ab
|
750 |
+
>>> from sympy import symbols
|
751 |
+
>>> a, b = symbols('a b')
|
752 |
+
>>> geometric_conj_ab(a, b)
|
753 |
+
a*b/(a + b)
|
754 |
+
"""
|
755 |
+
a, b = map(sympify, (a, b))
|
756 |
+
if a.is_infinite or b.is_infinite:
|
757 |
+
return a if b.is_infinite else b
|
758 |
+
else:
|
759 |
+
return a*b/(a + b)
|
760 |
+
|
761 |
+
|
762 |
+
def geometric_conj_af(a, f):
|
763 |
+
"""
|
764 |
+
Conjugation relation for geometrical beams under paraxial conditions.
|
765 |
+
|
766 |
+
Explanation
|
767 |
+
===========
|
768 |
+
|
769 |
+
Takes the object distance (for geometric_conj_af) or the image distance
|
770 |
+
(for geometric_conj_bf) to the optical element and the focal distance.
|
771 |
+
Then it returns the other distance needed for conjugation.
|
772 |
+
|
773 |
+
See Also
|
774 |
+
========
|
775 |
+
|
776 |
+
geometric_conj_ab
|
777 |
+
|
778 |
+
Examples
|
779 |
+
========
|
780 |
+
|
781 |
+
>>> from sympy.physics.optics.gaussopt import geometric_conj_af, geometric_conj_bf
|
782 |
+
>>> from sympy import symbols
|
783 |
+
>>> a, b, f = symbols('a b f')
|
784 |
+
>>> geometric_conj_af(a, f)
|
785 |
+
a*f/(a - f)
|
786 |
+
>>> geometric_conj_bf(b, f)
|
787 |
+
b*f/(b - f)
|
788 |
+
"""
|
789 |
+
a, f = map(sympify, (a, f))
|
790 |
+
return -geometric_conj_ab(a, -f)
|
791 |
+
|
792 |
+
geometric_conj_bf = geometric_conj_af
|
793 |
+
|
794 |
+
|
795 |
+
def gaussian_conj(s_in, z_r_in, f):
|
796 |
+
"""
|
797 |
+
Conjugation relation for gaussian beams.
|
798 |
+
|
799 |
+
Parameters
|
800 |
+
==========
|
801 |
+
|
802 |
+
s_in :
|
803 |
+
The distance to optical element from the waist.
|
804 |
+
z_r_in :
|
805 |
+
The rayleigh range of the incident beam.
|
806 |
+
f :
|
807 |
+
The focal length of the optical element.
|
808 |
+
|
809 |
+
Returns
|
810 |
+
=======
|
811 |
+
|
812 |
+
a tuple containing (s_out, z_r_out, m)
|
813 |
+
s_out :
|
814 |
+
The distance between the new waist and the optical element.
|
815 |
+
z_r_out :
|
816 |
+
The rayleigh range of the emergent beam.
|
817 |
+
m :
|
818 |
+
The ration between the new and the old waists.
|
819 |
+
|
820 |
+
Examples
|
821 |
+
========
|
822 |
+
|
823 |
+
>>> from sympy.physics.optics import gaussian_conj
|
824 |
+
>>> from sympy import symbols
|
825 |
+
>>> s_in, z_r_in, f = symbols('s_in z_r_in f')
|
826 |
+
|
827 |
+
>>> gaussian_conj(s_in, z_r_in, f)[0]
|
828 |
+
1/(-1/(s_in + z_r_in**2/(-f + s_in)) + 1/f)
|
829 |
+
|
830 |
+
>>> gaussian_conj(s_in, z_r_in, f)[1]
|
831 |
+
z_r_in/(1 - s_in**2/f**2 + z_r_in**2/f**2)
|
832 |
+
|
833 |
+
>>> gaussian_conj(s_in, z_r_in, f)[2]
|
834 |
+
1/sqrt(1 - s_in**2/f**2 + z_r_in**2/f**2)
|
835 |
+
"""
|
836 |
+
s_in, z_r_in, f = map(sympify, (s_in, z_r_in, f))
|
837 |
+
s_out = 1 / ( -1/(s_in + z_r_in**2/(s_in - f)) + 1/f )
|
838 |
+
m = 1/sqrt((1 - (s_in/f)**2) + (z_r_in/f)**2)
|
839 |
+
z_r_out = z_r_in / ((1 - (s_in/f)**2) + (z_r_in/f)**2)
|
840 |
+
return (s_out, z_r_out, m)
|
841 |
+
|
842 |
+
|
843 |
+
def conjugate_gauss_beams(wavelen, waist_in, waist_out, **kwargs):
|
844 |
+
"""
|
845 |
+
Find the optical setup conjugating the object/image waists.
|
846 |
+
|
847 |
+
Parameters
|
848 |
+
==========
|
849 |
+
|
850 |
+
wavelen :
|
851 |
+
The wavelength of the beam.
|
852 |
+
waist_in and waist_out :
|
853 |
+
The waists to be conjugated.
|
854 |
+
f :
|
855 |
+
The focal distance of the element used in the conjugation.
|
856 |
+
|
857 |
+
Returns
|
858 |
+
=======
|
859 |
+
|
860 |
+
a tuple containing (s_in, s_out, f)
|
861 |
+
s_in :
|
862 |
+
The distance before the optical element.
|
863 |
+
s_out :
|
864 |
+
The distance after the optical element.
|
865 |
+
f :
|
866 |
+
The focal distance of the optical element.
|
867 |
+
|
868 |
+
Examples
|
869 |
+
========
|
870 |
+
|
871 |
+
>>> from sympy.physics.optics import conjugate_gauss_beams
|
872 |
+
>>> from sympy import symbols, factor
|
873 |
+
>>> l, w_i, w_o, f = symbols('l w_i w_o f')
|
874 |
+
|
875 |
+
>>> conjugate_gauss_beams(l, w_i, w_o, f=f)[0]
|
876 |
+
f*(1 - sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)))
|
877 |
+
|
878 |
+
>>> factor(conjugate_gauss_beams(l, w_i, w_o, f=f)[1])
|
879 |
+
f*w_o**2*(w_i**2/w_o**2 - sqrt(w_i**2/w_o**2 -
|
880 |
+
pi**2*w_i**4/(f**2*l**2)))/w_i**2
|
881 |
+
|
882 |
+
>>> conjugate_gauss_beams(l, w_i, w_o, f=f)[2]
|
883 |
+
f
|
884 |
+
"""
|
885 |
+
#TODO add the other possible arguments
|
886 |
+
wavelen, waist_in, waist_out = map(sympify, (wavelen, waist_in, waist_out))
|
887 |
+
m = waist_out / waist_in
|
888 |
+
z = waist2rayleigh(waist_in, wavelen)
|
889 |
+
if len(kwargs) != 1:
|
890 |
+
raise ValueError("The function expects only one named argument")
|
891 |
+
elif 'dist' in kwargs:
|
892 |
+
raise NotImplementedError(filldedent('''
|
893 |
+
Currently only focal length is supported as a parameter'''))
|
894 |
+
elif 'f' in kwargs:
|
895 |
+
f = sympify(kwargs['f'])
|
896 |
+
s_in = f * (1 - sqrt(1/m**2 - z**2/f**2))
|
897 |
+
s_out = gaussian_conj(s_in, z, f)[0]
|
898 |
+
elif 's_in' in kwargs:
|
899 |
+
raise NotImplementedError(filldedent('''
|
900 |
+
Currently only focal length is supported as a parameter'''))
|
901 |
+
else:
|
902 |
+
raise ValueError(filldedent('''
|
903 |
+
The functions expects the focal length as a named argument'''))
|
904 |
+
return (s_in, s_out, f)
|
905 |
+
|
906 |
+
#TODO
|
907 |
+
#def plot_beam():
|
908 |
+
# """Plot the beam radius as it propagates in space."""
|
909 |
+
# pass
|
910 |
+
|
911 |
+
#TODO
|
912 |
+
#def plot_beam_conjugation():
|
913 |
+
# """
|
914 |
+
# Plot the intersection of two beams.
|
915 |
+
#
|
916 |
+
# Represents the conjugation relation.
|
917 |
+
#
|
918 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
**Contains**
|
3 |
+
|
4 |
+
* Medium
|
5 |
+
"""
|
6 |
+
from sympy.physics.units import second, meter, kilogram, ampere
|
7 |
+
|
8 |
+
__all__ = ['Medium']
|
9 |
+
|
10 |
+
from sympy.core.basic import Basic
|
11 |
+
from sympy.core.symbol import Str
|
12 |
+
from sympy.core.sympify import _sympify
|
13 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
14 |
+
from sympy.physics.units import speed_of_light, u0, e0
|
15 |
+
|
16 |
+
|
17 |
+
c = speed_of_light.convert_to(meter/second)
|
18 |
+
_e0mksa = e0.convert_to(ampere**2*second**4/(kilogram*meter**3))
|
19 |
+
_u0mksa = u0.convert_to(meter*kilogram/(ampere**2*second**2))
|
20 |
+
|
21 |
+
|
22 |
+
class Medium(Basic):
|
23 |
+
|
24 |
+
"""
|
25 |
+
This class represents an optical medium. The prime reason to implement this is
|
26 |
+
to facilitate refraction, Fermat's principle, etc.
|
27 |
+
|
28 |
+
Explanation
|
29 |
+
===========
|
30 |
+
|
31 |
+
An optical medium is a material through which electromagnetic waves propagate.
|
32 |
+
The permittivity and permeability of the medium define how electromagnetic
|
33 |
+
waves propagate in it.
|
34 |
+
|
35 |
+
|
36 |
+
Parameters
|
37 |
+
==========
|
38 |
+
|
39 |
+
name: string
|
40 |
+
The display name of the Medium.
|
41 |
+
|
42 |
+
permittivity: Sympifyable
|
43 |
+
Electric permittivity of the space.
|
44 |
+
|
45 |
+
permeability: Sympifyable
|
46 |
+
Magnetic permeability of the space.
|
47 |
+
|
48 |
+
n: Sympifyable
|
49 |
+
Index of refraction of the medium.
|
50 |
+
|
51 |
+
|
52 |
+
Examples
|
53 |
+
========
|
54 |
+
|
55 |
+
>>> from sympy.abc import epsilon, mu
|
56 |
+
>>> from sympy.physics.optics import Medium
|
57 |
+
>>> m1 = Medium('m1')
|
58 |
+
>>> m2 = Medium('m2', epsilon, mu)
|
59 |
+
>>> m1.intrinsic_impedance
|
60 |
+
149896229*pi*kilogram*meter**2/(1250000*ampere**2*second**3)
|
61 |
+
>>> m2.refractive_index
|
62 |
+
299792458*meter*sqrt(epsilon*mu)/second
|
63 |
+
|
64 |
+
|
65 |
+
References
|
66 |
+
==========
|
67 |
+
|
68 |
+
.. [1] https://en.wikipedia.org/wiki/Optical_medium
|
69 |
+
|
70 |
+
"""
|
71 |
+
|
72 |
+
def __new__(cls, name, permittivity=None, permeability=None, n=None):
|
73 |
+
if not isinstance(name, Str):
|
74 |
+
name = Str(name)
|
75 |
+
|
76 |
+
permittivity = _sympify(permittivity) if permittivity is not None else permittivity
|
77 |
+
permeability = _sympify(permeability) if permeability is not None else permeability
|
78 |
+
n = _sympify(n) if n is not None else n
|
79 |
+
|
80 |
+
if n is not None:
|
81 |
+
if permittivity is not None and permeability is None:
|
82 |
+
permeability = n**2/(c**2*permittivity)
|
83 |
+
return MediumPP(name, permittivity, permeability)
|
84 |
+
elif permeability is not None and permittivity is None:
|
85 |
+
permittivity = n**2/(c**2*permeability)
|
86 |
+
return MediumPP(name, permittivity, permeability)
|
87 |
+
elif permittivity is not None and permittivity is not None:
|
88 |
+
raise ValueError("Specifying all of permittivity, permeability, and n is not allowed")
|
89 |
+
else:
|
90 |
+
return MediumN(name, n)
|
91 |
+
elif permittivity is not None and permeability is not None:
|
92 |
+
return MediumPP(name, permittivity, permeability)
|
93 |
+
elif permittivity is None and permeability is None:
|
94 |
+
return MediumPP(name, _e0mksa, _u0mksa)
|
95 |
+
else:
|
96 |
+
raise ValueError("Arguments are underspecified. Either specify n or any two of permittivity, "
|
97 |
+
"permeability, and n")
|
98 |
+
|
99 |
+
@property
|
100 |
+
def name(self):
|
101 |
+
return self.args[0]
|
102 |
+
|
103 |
+
@property
|
104 |
+
def speed(self):
|
105 |
+
"""
|
106 |
+
Returns speed of the electromagnetic wave travelling in the medium.
|
107 |
+
|
108 |
+
Examples
|
109 |
+
========
|
110 |
+
|
111 |
+
>>> from sympy.physics.optics import Medium
|
112 |
+
>>> m = Medium('m')
|
113 |
+
>>> m.speed
|
114 |
+
299792458*meter/second
|
115 |
+
>>> m2 = Medium('m2', n=1)
|
116 |
+
>>> m.speed == m2.speed
|
117 |
+
True
|
118 |
+
|
119 |
+
"""
|
120 |
+
return c / self.n
|
121 |
+
|
122 |
+
@property
|
123 |
+
def refractive_index(self):
|
124 |
+
"""
|
125 |
+
Returns refractive index of the medium.
|
126 |
+
|
127 |
+
Examples
|
128 |
+
========
|
129 |
+
|
130 |
+
>>> from sympy.physics.optics import Medium
|
131 |
+
>>> m = Medium('m')
|
132 |
+
>>> m.refractive_index
|
133 |
+
1
|
134 |
+
|
135 |
+
"""
|
136 |
+
return (c/self.speed)
|
137 |
+
|
138 |
+
|
139 |
+
class MediumN(Medium):
|
140 |
+
|
141 |
+
"""
|
142 |
+
Represents an optical medium for which only the refractive index is known.
|
143 |
+
Useful for simple ray optics.
|
144 |
+
|
145 |
+
This class should never be instantiated directly.
|
146 |
+
Instead it should be instantiated indirectly by instantiating Medium with
|
147 |
+
only n specified.
|
148 |
+
|
149 |
+
Examples
|
150 |
+
========
|
151 |
+
>>> from sympy.physics.optics import Medium
|
152 |
+
>>> m = Medium('m', n=2)
|
153 |
+
>>> m
|
154 |
+
MediumN(Str('m'), 2)
|
155 |
+
"""
|
156 |
+
|
157 |
+
def __new__(cls, name, n):
|
158 |
+
obj = super(Medium, cls).__new__(cls, name, n)
|
159 |
+
return obj
|
160 |
+
|
161 |
+
@property
|
162 |
+
def n(self):
|
163 |
+
return self.args[1]
|
164 |
+
|
165 |
+
|
166 |
+
class MediumPP(Medium):
|
167 |
+
"""
|
168 |
+
Represents an optical medium for which the permittivity and permeability are known.
|
169 |
+
|
170 |
+
This class should never be instantiated directly. Instead it should be
|
171 |
+
instantiated indirectly by instantiating Medium with any two of
|
172 |
+
permittivity, permeability, and n specified, or by not specifying any
|
173 |
+
of permittivity, permeability, or n, in which case default values for
|
174 |
+
permittivity and permeability will be used.
|
175 |
+
|
176 |
+
Examples
|
177 |
+
========
|
178 |
+
>>> from sympy.physics.optics import Medium
|
179 |
+
>>> from sympy.abc import epsilon, mu
|
180 |
+
>>> m1 = Medium('m1', permittivity=epsilon, permeability=mu)
|
181 |
+
>>> m1
|
182 |
+
MediumPP(Str('m1'), epsilon, mu)
|
183 |
+
>>> m2 = Medium('m2')
|
184 |
+
>>> m2
|
185 |
+
MediumPP(Str('m2'), 625000*ampere**2*second**4/(22468879468420441*pi*kilogram*meter**3), pi*kilogram*meter/(2500000*ampere**2*second**2))
|
186 |
+
"""
|
187 |
+
|
188 |
+
|
189 |
+
def __new__(cls, name, permittivity, permeability):
|
190 |
+
obj = super(Medium, cls).__new__(cls, name, permittivity, permeability)
|
191 |
+
return obj
|
192 |
+
|
193 |
+
@property
|
194 |
+
def intrinsic_impedance(self):
|
195 |
+
"""
|
196 |
+
Returns intrinsic impedance of the medium.
|
197 |
+
|
198 |
+
Explanation
|
199 |
+
===========
|
200 |
+
|
201 |
+
The intrinsic impedance of a medium is the ratio of the
|
202 |
+
transverse components of the electric and magnetic fields
|
203 |
+
of the electromagnetic wave travelling in the medium.
|
204 |
+
In a region with no electrical conductivity it simplifies
|
205 |
+
to the square root of ratio of magnetic permeability to
|
206 |
+
electric permittivity.
|
207 |
+
|
208 |
+
Examples
|
209 |
+
========
|
210 |
+
|
211 |
+
>>> from sympy.physics.optics import Medium
|
212 |
+
>>> m = Medium('m')
|
213 |
+
>>> m.intrinsic_impedance
|
214 |
+
149896229*pi*kilogram*meter**2/(1250000*ampere**2*second**3)
|
215 |
+
|
216 |
+
"""
|
217 |
+
return sqrt(self.permeability / self.permittivity)
|
218 |
+
|
219 |
+
@property
|
220 |
+
def permittivity(self):
|
221 |
+
"""
|
222 |
+
Returns electric permittivity of the medium.
|
223 |
+
|
224 |
+
Examples
|
225 |
+
========
|
226 |
+
|
227 |
+
>>> from sympy.physics.optics import Medium
|
228 |
+
>>> m = Medium('m')
|
229 |
+
>>> m.permittivity
|
230 |
+
625000*ampere**2*second**4/(22468879468420441*pi*kilogram*meter**3)
|
231 |
+
|
232 |
+
"""
|
233 |
+
return self.args[1]
|
234 |
+
|
235 |
+
@property
|
236 |
+
def permeability(self):
|
237 |
+
"""
|
238 |
+
Returns magnetic permeability of the medium.
|
239 |
+
|
240 |
+
Examples
|
241 |
+
========
|
242 |
+
|
243 |
+
>>> from sympy.physics.optics import Medium
|
244 |
+
>>> m = Medium('m')
|
245 |
+
>>> m.permeability
|
246 |
+
pi*kilogram*meter/(2500000*ampere**2*second**2)
|
247 |
+
|
248 |
+
"""
|
249 |
+
return self.args[2]
|
250 |
+
|
251 |
+
@property
|
252 |
+
def n(self):
|
253 |
+
return c*sqrt(self.permittivity*self.permeability)
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/polarization.py
ADDED
@@ -0,0 +1,732 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
"""
|
4 |
+
The module implements routines to model the polarization of optical fields
|
5 |
+
and can be used to calculate the effects of polarization optical elements on
|
6 |
+
the fields.
|
7 |
+
|
8 |
+
- Jones vectors.
|
9 |
+
|
10 |
+
- Stokes vectors.
|
11 |
+
|
12 |
+
- Jones matrices.
|
13 |
+
|
14 |
+
- Mueller matrices.
|
15 |
+
|
16 |
+
Examples
|
17 |
+
========
|
18 |
+
|
19 |
+
We calculate a generic Jones vector:
|
20 |
+
|
21 |
+
>>> from sympy import symbols, pprint, zeros, simplify
|
22 |
+
>>> from sympy.physics.optics.polarization import (jones_vector, stokes_vector,
|
23 |
+
... half_wave_retarder, polarizing_beam_splitter, jones_2_stokes)
|
24 |
+
|
25 |
+
>>> psi, chi, p, I0 = symbols("psi, chi, p, I0", real=True)
|
26 |
+
>>> x0 = jones_vector(psi, chi)
|
27 |
+
>>> pprint(x0, use_unicode=True)
|
28 |
+
⎡-ⅈ⋅sin(χ)⋅sin(ψ) + cos(χ)⋅cos(ψ)⎤
|
29 |
+
⎢ ⎥
|
30 |
+
⎣ⅈ⋅sin(χ)⋅cos(ψ) + sin(ψ)⋅cos(χ) ⎦
|
31 |
+
|
32 |
+
And the more general Stokes vector:
|
33 |
+
|
34 |
+
>>> s0 = stokes_vector(psi, chi, p, I0)
|
35 |
+
>>> pprint(s0, use_unicode=True)
|
36 |
+
⎡ I₀ ⎤
|
37 |
+
⎢ ⎥
|
38 |
+
⎢I₀⋅p⋅cos(2⋅χ)⋅cos(2⋅ψ)⎥
|
39 |
+
⎢ ⎥
|
40 |
+
⎢I₀⋅p⋅sin(2⋅ψ)⋅cos(2⋅χ)⎥
|
41 |
+
⎢ ⎥
|
42 |
+
⎣ I₀⋅p⋅sin(2⋅χ) ⎦
|
43 |
+
|
44 |
+
We calculate how the Jones vector is modified by a half-wave plate:
|
45 |
+
|
46 |
+
>>> alpha = symbols("alpha", real=True)
|
47 |
+
>>> HWP = half_wave_retarder(alpha)
|
48 |
+
>>> x1 = simplify(HWP*x0)
|
49 |
+
|
50 |
+
We calculate the very common operation of passing a beam through a half-wave
|
51 |
+
plate and then through a polarizing beam-splitter. We do this by putting this
|
52 |
+
Jones vector as the first entry of a two-Jones-vector state that is transformed
|
53 |
+
by a 4x4 Jones matrix modelling the polarizing beam-splitter to get the
|
54 |
+
transmitted and reflected Jones vectors:
|
55 |
+
|
56 |
+
>>> PBS = polarizing_beam_splitter()
|
57 |
+
>>> X1 = zeros(4, 1)
|
58 |
+
>>> X1[:2, :] = x1
|
59 |
+
>>> X2 = PBS*X1
|
60 |
+
>>> transmitted_port = X2[:2, :]
|
61 |
+
>>> reflected_port = X2[2:, :]
|
62 |
+
|
63 |
+
This allows us to calculate how the power in both ports depends on the initial
|
64 |
+
polarization:
|
65 |
+
|
66 |
+
>>> transmitted_power = jones_2_stokes(transmitted_port)[0]
|
67 |
+
>>> reflected_power = jones_2_stokes(reflected_port)[0]
|
68 |
+
>>> print(transmitted_power)
|
69 |
+
cos(-2*alpha + chi + psi)**2/2 + cos(2*alpha + chi - psi)**2/2
|
70 |
+
|
71 |
+
|
72 |
+
>>> print(reflected_power)
|
73 |
+
sin(-2*alpha + chi + psi)**2/2 + sin(2*alpha + chi - psi)**2/2
|
74 |
+
|
75 |
+
Please see the description of the individual functions for further
|
76 |
+
details and examples.
|
77 |
+
|
78 |
+
References
|
79 |
+
==========
|
80 |
+
|
81 |
+
.. [1] https://en.wikipedia.org/wiki/Jones_calculus
|
82 |
+
.. [2] https://en.wikipedia.org/wiki/Mueller_calculus
|
83 |
+
.. [3] https://en.wikipedia.org/wiki/Stokes_parameters
|
84 |
+
|
85 |
+
"""
|
86 |
+
|
87 |
+
from sympy.core.numbers import (I, pi)
|
88 |
+
from sympy.functions.elementary.complexes import (Abs, im, re)
|
89 |
+
from sympy.functions.elementary.exponential import exp
|
90 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
91 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
92 |
+
from sympy.matrices.dense import Matrix
|
93 |
+
from sympy.simplify.simplify import simplify
|
94 |
+
from sympy.physics.quantum import TensorProduct
|
95 |
+
|
96 |
+
|
97 |
+
def jones_vector(psi, chi):
|
98 |
+
"""A Jones vector corresponding to a polarization ellipse with `psi` tilt,
|
99 |
+
and `chi` circularity.
|
100 |
+
|
101 |
+
Parameters
|
102 |
+
==========
|
103 |
+
|
104 |
+
psi : numeric type or SymPy Symbol
|
105 |
+
The tilt of the polarization relative to the `x` axis.
|
106 |
+
|
107 |
+
chi : numeric type or SymPy Symbol
|
108 |
+
The angle adjacent to the mayor axis of the polarization ellipse.
|
109 |
+
|
110 |
+
|
111 |
+
Returns
|
112 |
+
=======
|
113 |
+
|
114 |
+
Matrix :
|
115 |
+
A Jones vector.
|
116 |
+
|
117 |
+
Examples
|
118 |
+
========
|
119 |
+
|
120 |
+
The axes on the Poincaré sphere.
|
121 |
+
|
122 |
+
>>> from sympy import pprint, symbols, pi
|
123 |
+
>>> from sympy.physics.optics.polarization import jones_vector
|
124 |
+
>>> psi, chi = symbols("psi, chi", real=True)
|
125 |
+
|
126 |
+
A general Jones vector.
|
127 |
+
|
128 |
+
>>> pprint(jones_vector(psi, chi), use_unicode=True)
|
129 |
+
⎡-ⅈ⋅sin(χ)⋅sin(ψ) + cos(χ)⋅cos(ψ)⎤
|
130 |
+
⎢ ⎥
|
131 |
+
⎣ⅈ⋅sin(χ)⋅cos(ψ) + sin(ψ)⋅cos(χ) ⎦
|
132 |
+
|
133 |
+
Horizontal polarization.
|
134 |
+
|
135 |
+
>>> pprint(jones_vector(0, 0), use_unicode=True)
|
136 |
+
⎡1⎤
|
137 |
+
⎢ ⎥
|
138 |
+
⎣0⎦
|
139 |
+
|
140 |
+
Vertical polarization.
|
141 |
+
|
142 |
+
>>> pprint(jones_vector(pi/2, 0), use_unicode=True)
|
143 |
+
⎡0⎤
|
144 |
+
⎢ ⎥
|
145 |
+
⎣1⎦
|
146 |
+
|
147 |
+
Diagonal polarization.
|
148 |
+
|
149 |
+
>>> pprint(jones_vector(pi/4, 0), use_unicode=True)
|
150 |
+
⎡√2⎤
|
151 |
+
⎢──⎥
|
152 |
+
⎢2 ⎥
|
153 |
+
⎢ ⎥
|
154 |
+
⎢√2⎥
|
155 |
+
⎢──⎥
|
156 |
+
⎣2 ⎦
|
157 |
+
|
158 |
+
Anti-diagonal polarization.
|
159 |
+
|
160 |
+
>>> pprint(jones_vector(-pi/4, 0), use_unicode=True)
|
161 |
+
⎡ √2 ⎤
|
162 |
+
⎢ ── ⎥
|
163 |
+
⎢ 2 ⎥
|
164 |
+
⎢ ⎥
|
165 |
+
⎢-√2 ⎥
|
166 |
+
⎢────⎥
|
167 |
+
⎣ 2 ⎦
|
168 |
+
|
169 |
+
Right-hand circular polarization.
|
170 |
+
|
171 |
+
>>> pprint(jones_vector(0, pi/4), use_unicode=True)
|
172 |
+
⎡ √2 ⎤
|
173 |
+
⎢ ── ⎥
|
174 |
+
⎢ 2 ⎥
|
175 |
+
⎢ ⎥
|
176 |
+
⎢√2⋅ⅈ⎥
|
177 |
+
⎢────⎥
|
178 |
+
⎣ 2 ⎦
|
179 |
+
|
180 |
+
Left-hand circular polarization.
|
181 |
+
|
182 |
+
>>> pprint(jones_vector(0, -pi/4), use_unicode=True)
|
183 |
+
⎡ √2 ⎤
|
184 |
+
⎢ ── ⎥
|
185 |
+
⎢ 2 ⎥
|
186 |
+
⎢ ⎥
|
187 |
+
⎢-√2⋅ⅈ ⎥
|
188 |
+
⎢──────⎥
|
189 |
+
⎣ 2 ⎦
|
190 |
+
|
191 |
+
"""
|
192 |
+
return Matrix([-I*sin(chi)*sin(psi) + cos(chi)*cos(psi),
|
193 |
+
I*sin(chi)*cos(psi) + sin(psi)*cos(chi)])
|
194 |
+
|
195 |
+
|
196 |
+
def stokes_vector(psi, chi, p=1, I=1):
|
197 |
+
"""A Stokes vector corresponding to a polarization ellipse with ``psi``
|
198 |
+
tilt, and ``chi`` circularity.
|
199 |
+
|
200 |
+
Parameters
|
201 |
+
==========
|
202 |
+
|
203 |
+
psi : numeric type or SymPy Symbol
|
204 |
+
The tilt of the polarization relative to the ``x`` axis.
|
205 |
+
chi : numeric type or SymPy Symbol
|
206 |
+
The angle adjacent to the mayor axis of the polarization ellipse.
|
207 |
+
p : numeric type or SymPy Symbol
|
208 |
+
The degree of polarization.
|
209 |
+
I : numeric type or SymPy Symbol
|
210 |
+
The intensity of the field.
|
211 |
+
|
212 |
+
|
213 |
+
Returns
|
214 |
+
=======
|
215 |
+
|
216 |
+
Matrix :
|
217 |
+
A Stokes vector.
|
218 |
+
|
219 |
+
Examples
|
220 |
+
========
|
221 |
+
|
222 |
+
The axes on the Poincaré sphere.
|
223 |
+
|
224 |
+
>>> from sympy import pprint, symbols, pi
|
225 |
+
>>> from sympy.physics.optics.polarization import stokes_vector
|
226 |
+
>>> psi, chi, p, I = symbols("psi, chi, p, I", real=True)
|
227 |
+
>>> pprint(stokes_vector(psi, chi, p, I), use_unicode=True)
|
228 |
+
⎡ I ⎤
|
229 |
+
⎢ ⎥
|
230 |
+
⎢I⋅p⋅cos(2⋅χ)⋅cos(2⋅ψ)⎥
|
231 |
+
⎢ ⎥
|
232 |
+
⎢I⋅p⋅sin(2⋅ψ)⋅cos(2⋅χ)⎥
|
233 |
+
⎢ ⎥
|
234 |
+
⎣ I⋅p⋅sin(2⋅χ) ⎦
|
235 |
+
|
236 |
+
|
237 |
+
Horizontal polarization
|
238 |
+
|
239 |
+
>>> pprint(stokes_vector(0, 0), use_unicode=True)
|
240 |
+
⎡1⎤
|
241 |
+
⎢ ⎥
|
242 |
+
⎢1⎥
|
243 |
+
⎢ ⎥
|
244 |
+
⎢0⎥
|
245 |
+
⎢ ⎥
|
246 |
+
⎣0⎦
|
247 |
+
|
248 |
+
Vertical polarization
|
249 |
+
|
250 |
+
>>> pprint(stokes_vector(pi/2, 0), use_unicode=True)
|
251 |
+
⎡1 ⎤
|
252 |
+
⎢ ⎥
|
253 |
+
⎢-1⎥
|
254 |
+
⎢ ⎥
|
255 |
+
⎢0 ⎥
|
256 |
+
⎢ ⎥
|
257 |
+
⎣0 ⎦
|
258 |
+
|
259 |
+
Diagonal polarization
|
260 |
+
|
261 |
+
>>> pprint(stokes_vector(pi/4, 0), use_unicode=True)
|
262 |
+
⎡1⎤
|
263 |
+
⎢ ⎥
|
264 |
+
⎢0⎥
|
265 |
+
⎢ ⎥
|
266 |
+
⎢1⎥
|
267 |
+
⎢ ⎥
|
268 |
+
⎣0⎦
|
269 |
+
|
270 |
+
Anti-diagonal polarization
|
271 |
+
|
272 |
+
>>> pprint(stokes_vector(-pi/4, 0), use_unicode=True)
|
273 |
+
⎡1 ⎤
|
274 |
+
⎢ ⎥
|
275 |
+
⎢0 ⎥
|
276 |
+
⎢ ⎥
|
277 |
+
⎢-1⎥
|
278 |
+
⎢ ⎥
|
279 |
+
⎣0 ⎦
|
280 |
+
|
281 |
+
Right-hand circular polarization
|
282 |
+
|
283 |
+
>>> pprint(stokes_vector(0, pi/4), use_unicode=True)
|
284 |
+
⎡1⎤
|
285 |
+
⎢ ⎥
|
286 |
+
⎢0⎥
|
287 |
+
⎢ ⎥
|
288 |
+
⎢0⎥
|
289 |
+
⎢ ⎥
|
290 |
+
⎣1⎦
|
291 |
+
|
292 |
+
Left-hand circular polarization
|
293 |
+
|
294 |
+
>>> pprint(stokes_vector(0, -pi/4), use_unicode=True)
|
295 |
+
⎡1 ⎤
|
296 |
+
⎢ ⎥
|
297 |
+
⎢0 ⎥
|
298 |
+
⎢ ⎥
|
299 |
+
⎢0 ⎥
|
300 |
+
⎢ ⎥
|
301 |
+
⎣-1⎦
|
302 |
+
|
303 |
+
Unpolarized light
|
304 |
+
|
305 |
+
>>> pprint(stokes_vector(0, 0, 0), use_unicode=True)
|
306 |
+
⎡1⎤
|
307 |
+
⎢ ⎥
|
308 |
+
⎢0⎥
|
309 |
+
⎢ ⎥
|
310 |
+
⎢0⎥
|
311 |
+
⎢ ⎥
|
312 |
+
⎣0⎦
|
313 |
+
|
314 |
+
"""
|
315 |
+
S0 = I
|
316 |
+
S1 = I*p*cos(2*psi)*cos(2*chi)
|
317 |
+
S2 = I*p*sin(2*psi)*cos(2*chi)
|
318 |
+
S3 = I*p*sin(2*chi)
|
319 |
+
return Matrix([S0, S1, S2, S3])
|
320 |
+
|
321 |
+
|
322 |
+
def jones_2_stokes(e):
|
323 |
+
"""Return the Stokes vector for a Jones vector ``e``.
|
324 |
+
|
325 |
+
Parameters
|
326 |
+
==========
|
327 |
+
|
328 |
+
e : SymPy Matrix
|
329 |
+
A Jones vector.
|
330 |
+
|
331 |
+
Returns
|
332 |
+
=======
|
333 |
+
|
334 |
+
SymPy Matrix
|
335 |
+
A Jones vector.
|
336 |
+
|
337 |
+
Examples
|
338 |
+
========
|
339 |
+
|
340 |
+
The axes on the Poincaré sphere.
|
341 |
+
|
342 |
+
>>> from sympy import pprint, pi
|
343 |
+
>>> from sympy.physics.optics.polarization import jones_vector
|
344 |
+
>>> from sympy.physics.optics.polarization import jones_2_stokes
|
345 |
+
>>> H = jones_vector(0, 0)
|
346 |
+
>>> V = jones_vector(pi/2, 0)
|
347 |
+
>>> D = jones_vector(pi/4, 0)
|
348 |
+
>>> A = jones_vector(-pi/4, 0)
|
349 |
+
>>> R = jones_vector(0, pi/4)
|
350 |
+
>>> L = jones_vector(0, -pi/4)
|
351 |
+
>>> pprint([jones_2_stokes(e) for e in [H, V, D, A, R, L]],
|
352 |
+
... use_unicode=True)
|
353 |
+
⎡⎡1⎤ ⎡1 ⎤ ⎡1⎤ ⎡1 ⎤ ⎡1⎤ ⎡1 ⎤⎤
|
354 |
+
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎥
|
355 |
+
⎢⎢1⎥ ⎢-1⎥ ⎢0⎥ ⎢0 ⎥ ⎢0⎥ ⎢0 ⎥⎥
|
356 |
+
⎢⎢ ⎥, ⎢ ⎥, ⎢ ⎥, ⎢ ⎥, ⎢ ⎥, ⎢ ⎥⎥
|
357 |
+
⎢⎢0⎥ ⎢0 ⎥ ⎢1⎥ ⎢-1⎥ ⎢0⎥ ⎢0 ⎥⎥
|
358 |
+
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎥
|
359 |
+
⎣⎣0⎦ ⎣0 ⎦ ⎣0⎦ ⎣0 ⎦ ⎣1⎦ ⎣-1⎦⎦
|
360 |
+
|
361 |
+
"""
|
362 |
+
ex, ey = e
|
363 |
+
return Matrix([Abs(ex)**2 + Abs(ey)**2,
|
364 |
+
Abs(ex)**2 - Abs(ey)**2,
|
365 |
+
2*re(ex*ey.conjugate()),
|
366 |
+
-2*im(ex*ey.conjugate())])
|
367 |
+
|
368 |
+
|
369 |
+
def linear_polarizer(theta=0):
|
370 |
+
"""A linear polarizer Jones matrix with transmission axis at
|
371 |
+
an angle ``theta``.
|
372 |
+
|
373 |
+
Parameters
|
374 |
+
==========
|
375 |
+
|
376 |
+
theta : numeric type or SymPy Symbol
|
377 |
+
The angle of the transmission axis relative to the horizontal plane.
|
378 |
+
|
379 |
+
Returns
|
380 |
+
=======
|
381 |
+
|
382 |
+
SymPy Matrix
|
383 |
+
A Jones matrix representing the polarizer.
|
384 |
+
|
385 |
+
Examples
|
386 |
+
========
|
387 |
+
|
388 |
+
A generic polarizer.
|
389 |
+
|
390 |
+
>>> from sympy import pprint, symbols
|
391 |
+
>>> from sympy.physics.optics.polarization import linear_polarizer
|
392 |
+
>>> theta = symbols("theta", real=True)
|
393 |
+
>>> J = linear_polarizer(theta)
|
394 |
+
>>> pprint(J, use_unicode=True)
|
395 |
+
⎡ 2 ⎤
|
396 |
+
⎢ cos (θ) sin(θ)⋅cos(θ)⎥
|
397 |
+
⎢ ⎥
|
398 |
+
⎢ 2 ⎥
|
399 |
+
⎣sin(θ)⋅cos(θ) sin (θ) ⎦
|
400 |
+
|
401 |
+
|
402 |
+
"""
|
403 |
+
M = Matrix([[cos(theta)**2, sin(theta)*cos(theta)],
|
404 |
+
[sin(theta)*cos(theta), sin(theta)**2]])
|
405 |
+
return M
|
406 |
+
|
407 |
+
|
408 |
+
def phase_retarder(theta=0, delta=0):
|
409 |
+
"""A phase retarder Jones matrix with retardance ``delta`` at angle ``theta``.
|
410 |
+
|
411 |
+
Parameters
|
412 |
+
==========
|
413 |
+
|
414 |
+
theta : numeric type or SymPy Symbol
|
415 |
+
The angle of the fast axis relative to the horizontal plane.
|
416 |
+
delta : numeric type or SymPy Symbol
|
417 |
+
The phase difference between the fast and slow axes of the
|
418 |
+
transmitted light.
|
419 |
+
|
420 |
+
Returns
|
421 |
+
=======
|
422 |
+
|
423 |
+
SymPy Matrix :
|
424 |
+
A Jones matrix representing the retarder.
|
425 |
+
|
426 |
+
Examples
|
427 |
+
========
|
428 |
+
|
429 |
+
A generic retarder.
|
430 |
+
|
431 |
+
>>> from sympy import pprint, symbols
|
432 |
+
>>> from sympy.physics.optics.polarization import phase_retarder
|
433 |
+
>>> theta, delta = symbols("theta, delta", real=True)
|
434 |
+
>>> R = phase_retarder(theta, delta)
|
435 |
+
>>> pprint(R, use_unicode=True)
|
436 |
+
⎡ -ⅈ⋅δ -ⅈ⋅δ ⎤
|
437 |
+
⎢ ───── ───── ⎥
|
438 |
+
⎢⎛ ⅈ⋅δ 2 2 ⎞ 2 ⎛ ⅈ⋅δ⎞ 2 ⎥
|
439 |
+
⎢⎝ℯ ⋅sin (θ) + cos (θ)⎠⋅ℯ ⎝1 - ℯ ⎠⋅ℯ ⋅sin(θ)⋅cos(θ)⎥
|
440 |
+
⎢ ⎥
|
441 |
+
⎢ -ⅈ⋅δ -ⅈ⋅δ ⎥
|
442 |
+
⎢ ───── ─────⎥
|
443 |
+
⎢⎛ ⅈ⋅δ⎞ 2 ⎛ ⅈ⋅δ 2 2 ⎞ 2 ⎥
|
444 |
+
⎣⎝1 - ℯ ⎠⋅ℯ ⋅sin(θ)⋅cos(θ) ⎝ℯ ⋅cos (θ) + sin (θ)⎠⋅ℯ ⎦
|
445 |
+
|
446 |
+
"""
|
447 |
+
R = Matrix([[cos(theta)**2 + exp(I*delta)*sin(theta)**2,
|
448 |
+
(1-exp(I*delta))*cos(theta)*sin(theta)],
|
449 |
+
[(1-exp(I*delta))*cos(theta)*sin(theta),
|
450 |
+
sin(theta)**2 + exp(I*delta)*cos(theta)**2]])
|
451 |
+
return R*exp(-I*delta/2)
|
452 |
+
|
453 |
+
|
454 |
+
def half_wave_retarder(theta):
|
455 |
+
"""A half-wave retarder Jones matrix at angle ``theta``.
|
456 |
+
|
457 |
+
Parameters
|
458 |
+
==========
|
459 |
+
|
460 |
+
theta : numeric type or SymPy Symbol
|
461 |
+
The angle of the fast axis relative to the horizontal plane.
|
462 |
+
|
463 |
+
Returns
|
464 |
+
=======
|
465 |
+
|
466 |
+
SymPy Matrix
|
467 |
+
A Jones matrix representing the retarder.
|
468 |
+
|
469 |
+
Examples
|
470 |
+
========
|
471 |
+
|
472 |
+
A generic half-wave plate.
|
473 |
+
|
474 |
+
>>> from sympy import pprint, symbols
|
475 |
+
>>> from sympy.physics.optics.polarization import half_wave_retarder
|
476 |
+
>>> theta= symbols("theta", real=True)
|
477 |
+
>>> HWP = half_wave_retarder(theta)
|
478 |
+
>>> pprint(HWP, use_unicode=True)
|
479 |
+
⎡ ⎛ 2 2 ⎞ ⎤
|
480 |
+
⎢-ⅈ⋅⎝- sin (θ) + cos (θ)⎠ -2⋅ⅈ⋅sin(θ)⋅cos(θ) ⎥
|
481 |
+
⎢ ⎥
|
482 |
+
⎢ ⎛ 2 2 ⎞⎥
|
483 |
+
⎣ -2⋅ⅈ⋅sin(θ)⋅cos(θ) -ⅈ⋅⎝sin (θ) - cos (θ)⎠⎦
|
484 |
+
|
485 |
+
"""
|
486 |
+
return phase_retarder(theta, pi)
|
487 |
+
|
488 |
+
|
489 |
+
def quarter_wave_retarder(theta):
|
490 |
+
"""A quarter-wave retarder Jones matrix at angle ``theta``.
|
491 |
+
|
492 |
+
Parameters
|
493 |
+
==========
|
494 |
+
|
495 |
+
theta : numeric type or SymPy Symbol
|
496 |
+
The angle of the fast axis relative to the horizontal plane.
|
497 |
+
|
498 |
+
Returns
|
499 |
+
=======
|
500 |
+
|
501 |
+
SymPy Matrix
|
502 |
+
A Jones matrix representing the retarder.
|
503 |
+
|
504 |
+
Examples
|
505 |
+
========
|
506 |
+
|
507 |
+
A generic quarter-wave plate.
|
508 |
+
|
509 |
+
>>> from sympy import pprint, symbols
|
510 |
+
>>> from sympy.physics.optics.polarization import quarter_wave_retarder
|
511 |
+
>>> theta= symbols("theta", real=True)
|
512 |
+
>>> QWP = quarter_wave_retarder(theta)
|
513 |
+
>>> pprint(QWP, use_unicode=True)
|
514 |
+
⎡ -ⅈ⋅π -ⅈ⋅π ⎤
|
515 |
+
⎢ ───── ───── ⎥
|
516 |
+
⎢⎛ 2 2 ⎞ 4 4 ⎥
|
517 |
+
⎢⎝ⅈ⋅sin (θ) + cos (θ)⎠⋅ℯ (1 - ⅈ)⋅ℯ ⋅sin(θ)⋅cos(θ)⎥
|
518 |
+
⎢ ⎥
|
519 |
+
⎢ -ⅈ⋅π -ⅈ⋅π ⎥
|
520 |
+
⎢ ───── ─────⎥
|
521 |
+
⎢ 4 ⎛ 2 2 ⎞ 4 ⎥
|
522 |
+
⎣(1 - ⅈ)⋅ℯ ⋅sin(θ)⋅cos(θ) ⎝sin (θ) + ⅈ⋅cos (θ)⎠⋅ℯ ⎦
|
523 |
+
|
524 |
+
"""
|
525 |
+
return phase_retarder(theta, pi/2)
|
526 |
+
|
527 |
+
|
528 |
+
def transmissive_filter(T):
|
529 |
+
"""An attenuator Jones matrix with transmittance ``T``.
|
530 |
+
|
531 |
+
Parameters
|
532 |
+
==========
|
533 |
+
|
534 |
+
T : numeric type or SymPy Symbol
|
535 |
+
The transmittance of the attenuator.
|
536 |
+
|
537 |
+
Returns
|
538 |
+
=======
|
539 |
+
|
540 |
+
SymPy Matrix
|
541 |
+
A Jones matrix representing the filter.
|
542 |
+
|
543 |
+
Examples
|
544 |
+
========
|
545 |
+
|
546 |
+
A generic filter.
|
547 |
+
|
548 |
+
>>> from sympy import pprint, symbols
|
549 |
+
>>> from sympy.physics.optics.polarization import transmissive_filter
|
550 |
+
>>> T = symbols("T", real=True)
|
551 |
+
>>> NDF = transmissive_filter(T)
|
552 |
+
>>> pprint(NDF, use_unicode=True)
|
553 |
+
⎡√T 0 ⎤
|
554 |
+
⎢ ⎥
|
555 |
+
⎣0 √T⎦
|
556 |
+
|
557 |
+
"""
|
558 |
+
return Matrix([[sqrt(T), 0], [0, sqrt(T)]])
|
559 |
+
|
560 |
+
|
561 |
+
def reflective_filter(R):
|
562 |
+
"""A reflective filter Jones matrix with reflectance ``R``.
|
563 |
+
|
564 |
+
Parameters
|
565 |
+
==========
|
566 |
+
|
567 |
+
R : numeric type or SymPy Symbol
|
568 |
+
The reflectance of the filter.
|
569 |
+
|
570 |
+
Returns
|
571 |
+
=======
|
572 |
+
|
573 |
+
SymPy Matrix
|
574 |
+
A Jones matrix representing the filter.
|
575 |
+
|
576 |
+
Examples
|
577 |
+
========
|
578 |
+
|
579 |
+
A generic filter.
|
580 |
+
|
581 |
+
>>> from sympy import pprint, symbols
|
582 |
+
>>> from sympy.physics.optics.polarization import reflective_filter
|
583 |
+
>>> R = symbols("R", real=True)
|
584 |
+
>>> pprint(reflective_filter(R), use_unicode=True)
|
585 |
+
⎡√R 0 ⎤
|
586 |
+
⎢ ⎥
|
587 |
+
⎣0 -√R⎦
|
588 |
+
|
589 |
+
"""
|
590 |
+
return Matrix([[sqrt(R), 0], [0, -sqrt(R)]])
|
591 |
+
|
592 |
+
|
593 |
+
def mueller_matrix(J):
|
594 |
+
"""The Mueller matrix corresponding to Jones matrix `J`.
|
595 |
+
|
596 |
+
Parameters
|
597 |
+
==========
|
598 |
+
|
599 |
+
J : SymPy Matrix
|
600 |
+
A Jones matrix.
|
601 |
+
|
602 |
+
Returns
|
603 |
+
=======
|
604 |
+
|
605 |
+
SymPy Matrix
|
606 |
+
The corresponding Mueller matrix.
|
607 |
+
|
608 |
+
Examples
|
609 |
+
========
|
610 |
+
|
611 |
+
Generic optical components.
|
612 |
+
|
613 |
+
>>> from sympy import pprint, symbols
|
614 |
+
>>> from sympy.physics.optics.polarization import (mueller_matrix,
|
615 |
+
... linear_polarizer, half_wave_retarder, quarter_wave_retarder)
|
616 |
+
>>> theta = symbols("theta", real=True)
|
617 |
+
|
618 |
+
A linear_polarizer
|
619 |
+
|
620 |
+
>>> pprint(mueller_matrix(linear_polarizer(theta)), use_unicode=True)
|
621 |
+
⎡ cos(2⋅θ) sin(2⋅θ) ⎤
|
622 |
+
⎢ 1/2 ──────── ──────── 0⎥
|
623 |
+
⎢ 2 2 ⎥
|
624 |
+
⎢ ⎥
|
625 |
+
⎢cos(2⋅θ) cos(4⋅θ) 1 sin(4⋅θ) ⎥
|
626 |
+
⎢──────── ──────── + ─ ──────── 0⎥
|
627 |
+
⎢ 2 4 4 4 ⎥
|
628 |
+
⎢ ⎥
|
629 |
+
⎢sin(2⋅θ) sin(4⋅θ) 1 cos(4⋅θ) ⎥
|
630 |
+
⎢──────── ──────── ─ - ──────── 0⎥
|
631 |
+
⎢ 2 4 4 4 ⎥
|
632 |
+
⎢ ⎥
|
633 |
+
⎣ 0 0 0 0⎦
|
634 |
+
|
635 |
+
A half-wave plate
|
636 |
+
|
637 |
+
>>> pprint(mueller_matrix(half_wave_retarder(theta)), use_unicode=True)
|
638 |
+
⎡1 0 0 0 ⎤
|
639 |
+
⎢ ⎥
|
640 |
+
⎢ 4 2 ⎥
|
641 |
+
⎢0 8⋅sin (θ) - 8⋅sin (θ) + 1 sin(4⋅θ) 0 ⎥
|
642 |
+
⎢ ⎥
|
643 |
+
⎢ 4 2 ⎥
|
644 |
+
⎢0 sin(4⋅θ) - 8⋅sin (θ) + 8⋅sin (θ) - 1 0 ⎥
|
645 |
+
⎢ ⎥
|
646 |
+
⎣0 0 0 -1⎦
|
647 |
+
|
648 |
+
A quarter-wave plate
|
649 |
+
|
650 |
+
>>> pprint(mueller_matrix(quarter_wave_retarder(theta)), use_unicode=True)
|
651 |
+
⎡1 0 0 0 ⎤
|
652 |
+
⎢ ⎥
|
653 |
+
⎢ cos(4⋅θ) 1 sin(4⋅θ) ⎥
|
654 |
+
⎢0 ──────── + ─ ──────── -sin(2⋅θ)⎥
|
655 |
+
⎢ 2 2 2 ⎥
|
656 |
+
⎢ ⎥
|
657 |
+
⎢ sin(4⋅θ) 1 cos(4⋅θ) ⎥
|
658 |
+
⎢0 ──────── ─ - ──────── cos(2⋅θ) ⎥
|
659 |
+
⎢ 2 2 2 ⎥
|
660 |
+
⎢ ⎥
|
661 |
+
⎣0 sin(2⋅θ) -cos(2⋅θ) 0 ⎦
|
662 |
+
|
663 |
+
"""
|
664 |
+
A = Matrix([[1, 0, 0, 1],
|
665 |
+
[1, 0, 0, -1],
|
666 |
+
[0, 1, 1, 0],
|
667 |
+
[0, -I, I, 0]])
|
668 |
+
|
669 |
+
return simplify(A*TensorProduct(J, J.conjugate())*A.inv())
|
670 |
+
|
671 |
+
|
672 |
+
def polarizing_beam_splitter(Tp=1, Rs=1, Ts=0, Rp=0, phia=0, phib=0):
|
673 |
+
r"""A polarizing beam splitter Jones matrix at angle `theta`.
|
674 |
+
|
675 |
+
Parameters
|
676 |
+
==========
|
677 |
+
|
678 |
+
J : SymPy Matrix
|
679 |
+
A Jones matrix.
|
680 |
+
Tp : numeric type or SymPy Symbol
|
681 |
+
The transmissivity of the P-polarized component.
|
682 |
+
Rs : numeric type or SymPy Symbol
|
683 |
+
The reflectivity of the S-polarized component.
|
684 |
+
Ts : numeric type or SymPy Symbol
|
685 |
+
The transmissivity of the S-polarized component.
|
686 |
+
Rp : numeric type or SymPy Symbol
|
687 |
+
The reflectivity of the P-polarized component.
|
688 |
+
phia : numeric type or SymPy Symbol
|
689 |
+
The phase difference between transmitted and reflected component for
|
690 |
+
output mode a.
|
691 |
+
phib : numeric type or SymPy Symbol
|
692 |
+
The phase difference between transmitted and reflected component for
|
693 |
+
output mode b.
|
694 |
+
|
695 |
+
|
696 |
+
Returns
|
697 |
+
=======
|
698 |
+
|
699 |
+
SymPy Matrix
|
700 |
+
A 4x4 matrix representing the PBS. This matrix acts on a 4x1 vector
|
701 |
+
whose first two entries are the Jones vector on one of the PBS ports,
|
702 |
+
and the last two entries the Jones vector on the other port.
|
703 |
+
|
704 |
+
Examples
|
705 |
+
========
|
706 |
+
|
707 |
+
Generic polarizing beam-splitter.
|
708 |
+
|
709 |
+
>>> from sympy import pprint, symbols
|
710 |
+
>>> from sympy.physics.optics.polarization import polarizing_beam_splitter
|
711 |
+
>>> Ts, Rs, Tp, Rp = symbols(r"Ts, Rs, Tp, Rp", positive=True)
|
712 |
+
>>> phia, phib = symbols("phi_a, phi_b", real=True)
|
713 |
+
>>> PBS = polarizing_beam_splitter(Tp, Rs, Ts, Rp, phia, phib)
|
714 |
+
>>> pprint(PBS, use_unicode=False)
|
715 |
+
[ ____ ____ ]
|
716 |
+
[ \/ Tp 0 I*\/ Rp 0 ]
|
717 |
+
[ ]
|
718 |
+
[ ____ ____ I*phi_a]
|
719 |
+
[ 0 \/ Ts 0 -I*\/ Rs *e ]
|
720 |
+
[ ]
|
721 |
+
[ ____ ____ ]
|
722 |
+
[I*\/ Rp 0 \/ Tp 0 ]
|
723 |
+
[ ]
|
724 |
+
[ ____ I*phi_b ____ ]
|
725 |
+
[ 0 -I*\/ Rs *e 0 \/ Ts ]
|
726 |
+
|
727 |
+
"""
|
728 |
+
PBS = Matrix([[sqrt(Tp), 0, I*sqrt(Rp), 0],
|
729 |
+
[0, sqrt(Ts), 0, -I*sqrt(Rs)*exp(I*phia)],
|
730 |
+
[I*sqrt(Rp), 0, sqrt(Tp), 0],
|
731 |
+
[0, -I*sqrt(Rs)*exp(I*phib), 0, sqrt(Ts)]])
|
732 |
+
return PBS
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (199 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_gaussopt.cpython-310.pyc
ADDED
Binary file (4.11 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_medium.cpython-310.pyc
ADDED
Binary file (2.18 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_polarization.cpython-310.pyc
ADDED
Binary file (2.22 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_utils.cpython-310.pyc
ADDED
Binary file (8.24 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/__pycache__/test_waves.cpython-310.pyc
ADDED
Binary file (3.55 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_gaussopt.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.evalf import N
|
2 |
+
from sympy.core.numbers import (Float, I, oo, pi)
|
3 |
+
from sympy.core.symbol import symbols
|
4 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
5 |
+
from sympy.functions.elementary.trigonometric import atan2
|
6 |
+
from sympy.matrices.dense import Matrix
|
7 |
+
from sympy.polys.polytools import factor
|
8 |
+
|
9 |
+
from sympy.physics.optics import (BeamParameter, CurvedMirror,
|
10 |
+
CurvedRefraction, FlatMirror, FlatRefraction, FreeSpace, GeometricRay,
|
11 |
+
RayTransferMatrix, ThinLens, conjugate_gauss_beams,
|
12 |
+
gaussian_conj, geometric_conj_ab, geometric_conj_af, geometric_conj_bf,
|
13 |
+
rayleigh2waist, waist2rayleigh)
|
14 |
+
|
15 |
+
|
16 |
+
def streq(a, b):
|
17 |
+
return str(a) == str(b)
|
18 |
+
|
19 |
+
|
20 |
+
def test_gauss_opt():
|
21 |
+
mat = RayTransferMatrix(1, 2, 3, 4)
|
22 |
+
assert mat == Matrix([[1, 2], [3, 4]])
|
23 |
+
assert mat == RayTransferMatrix( Matrix([[1, 2], [3, 4]]) )
|
24 |
+
assert [mat.A, mat.B, mat.C, mat.D] == [1, 2, 3, 4]
|
25 |
+
|
26 |
+
d, f, h, n1, n2, R = symbols('d f h n1 n2 R')
|
27 |
+
lens = ThinLens(f)
|
28 |
+
assert lens == Matrix([[ 1, 0], [-1/f, 1]])
|
29 |
+
assert lens.C == -1/f
|
30 |
+
assert FreeSpace(d) == Matrix([[ 1, d], [0, 1]])
|
31 |
+
assert FlatRefraction(n1, n2) == Matrix([[1, 0], [0, n1/n2]])
|
32 |
+
assert CurvedRefraction(
|
33 |
+
R, n1, n2) == Matrix([[1, 0], [(n1 - n2)/(R*n2), n1/n2]])
|
34 |
+
assert FlatMirror() == Matrix([[1, 0], [0, 1]])
|
35 |
+
assert CurvedMirror(R) == Matrix([[ 1, 0], [-2/R, 1]])
|
36 |
+
assert ThinLens(f) == Matrix([[ 1, 0], [-1/f, 1]])
|
37 |
+
|
38 |
+
mul = CurvedMirror(R)*FreeSpace(d)
|
39 |
+
mul_mat = Matrix([[ 1, 0], [-2/R, 1]])*Matrix([[ 1, d], [0, 1]])
|
40 |
+
assert mul.A == mul_mat[0, 0]
|
41 |
+
assert mul.B == mul_mat[0, 1]
|
42 |
+
assert mul.C == mul_mat[1, 0]
|
43 |
+
assert mul.D == mul_mat[1, 1]
|
44 |
+
|
45 |
+
angle = symbols('angle')
|
46 |
+
assert GeometricRay(h, angle) == Matrix([[ h], [angle]])
|
47 |
+
assert FreeSpace(
|
48 |
+
d)*GeometricRay(h, angle) == Matrix([[angle*d + h], [angle]])
|
49 |
+
assert GeometricRay( Matrix( ((h,), (angle,)) ) ) == Matrix([[h], [angle]])
|
50 |
+
assert (FreeSpace(d)*GeometricRay(h, angle)).height == angle*d + h
|
51 |
+
assert (FreeSpace(d)*GeometricRay(h, angle)).angle == angle
|
52 |
+
|
53 |
+
p = BeamParameter(530e-9, 1, w=1e-3)
|
54 |
+
assert streq(p.q, 1 + 1.88679245283019*I*pi)
|
55 |
+
assert streq(N(p.q), 1.0 + 5.92753330865999*I)
|
56 |
+
assert streq(N(p.w_0), Float(0.00100000000000000))
|
57 |
+
assert streq(N(p.z_r), Float(5.92753330865999))
|
58 |
+
fs = FreeSpace(10)
|
59 |
+
p1 = fs*p
|
60 |
+
assert streq(N(p.w), Float(0.00101413072159615))
|
61 |
+
assert streq(N(p1.w), Float(0.00210803120913829))
|
62 |
+
|
63 |
+
w, wavelen = symbols('w wavelen')
|
64 |
+
assert waist2rayleigh(w, wavelen) == pi*w**2/wavelen
|
65 |
+
z_r, wavelen = symbols('z_r wavelen')
|
66 |
+
assert rayleigh2waist(z_r, wavelen) == sqrt(wavelen*z_r)/sqrt(pi)
|
67 |
+
|
68 |
+
a, b, f = symbols('a b f')
|
69 |
+
assert geometric_conj_ab(a, b) == a*b/(a + b)
|
70 |
+
assert geometric_conj_af(a, f) == a*f/(a - f)
|
71 |
+
assert geometric_conj_bf(b, f) == b*f/(b - f)
|
72 |
+
assert geometric_conj_ab(oo, b) == b
|
73 |
+
assert geometric_conj_ab(a, oo) == a
|
74 |
+
|
75 |
+
s_in, z_r_in, f = symbols('s_in z_r_in f')
|
76 |
+
assert gaussian_conj(
|
77 |
+
s_in, z_r_in, f)[0] == 1/(-1/(s_in + z_r_in**2/(-f + s_in)) + 1/f)
|
78 |
+
assert gaussian_conj(
|
79 |
+
s_in, z_r_in, f)[1] == z_r_in/(1 - s_in**2/f**2 + z_r_in**2/f**2)
|
80 |
+
assert gaussian_conj(
|
81 |
+
s_in, z_r_in, f)[2] == 1/sqrt(1 - s_in**2/f**2 + z_r_in**2/f**2)
|
82 |
+
|
83 |
+
l, w_i, w_o, f = symbols('l w_i w_o f')
|
84 |
+
assert conjugate_gauss_beams(l, w_i, w_o, f=f)[0] == f*(
|
85 |
+
-sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)) + 1)
|
86 |
+
assert factor(conjugate_gauss_beams(l, w_i, w_o, f=f)[1]) == f*w_o**2*(
|
87 |
+
w_i**2/w_o**2 - sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)))/w_i**2
|
88 |
+
assert conjugate_gauss_beams(l, w_i, w_o, f=f)[2] == f
|
89 |
+
|
90 |
+
z, l, w_0 = symbols('z l w_0', positive=True)
|
91 |
+
p = BeamParameter(l, z, w=w_0)
|
92 |
+
assert p.radius == z*(pi**2*w_0**4/(l**2*z**2) + 1)
|
93 |
+
assert p.w == w_0*sqrt(l**2*z**2/(pi**2*w_0**4) + 1)
|
94 |
+
assert p.w_0 == w_0
|
95 |
+
assert p.divergence == l/(pi*w_0)
|
96 |
+
assert p.gouy == atan2(z, pi*w_0**2/l)
|
97 |
+
assert p.waist_approximation_limit == 2*l/pi
|
98 |
+
|
99 |
+
p = BeamParameter(530e-9, 1, w=1e-3, n=2)
|
100 |
+
assert streq(p.q, 1 + 3.77358490566038*I*pi)
|
101 |
+
assert streq(N(p.z_r), Float(11.8550666173200))
|
102 |
+
assert streq(N(p.w_0), Float(0.00100000000000000))
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_medium.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
2 |
+
from sympy.physics.optics import Medium
|
3 |
+
from sympy.abc import epsilon, mu, n
|
4 |
+
from sympy.physics.units import speed_of_light, u0, e0, m, kg, s, A
|
5 |
+
|
6 |
+
from sympy.testing.pytest import raises
|
7 |
+
|
8 |
+
c = speed_of_light.convert_to(m/s)
|
9 |
+
e0 = e0.convert_to(A**2*s**4/(kg*m**3))
|
10 |
+
u0 = u0.convert_to(m*kg/(A**2*s**2))
|
11 |
+
|
12 |
+
|
13 |
+
def test_medium():
|
14 |
+
m1 = Medium('m1')
|
15 |
+
assert m1.intrinsic_impedance == sqrt(u0/e0)
|
16 |
+
assert m1.speed == 1/sqrt(e0*u0)
|
17 |
+
assert m1.refractive_index == c*sqrt(e0*u0)
|
18 |
+
assert m1.permittivity == e0
|
19 |
+
assert m1.permeability == u0
|
20 |
+
m2 = Medium('m2', epsilon, mu)
|
21 |
+
assert m2.intrinsic_impedance == sqrt(mu/epsilon)
|
22 |
+
assert m2.speed == 1/sqrt(epsilon*mu)
|
23 |
+
assert m2.refractive_index == c*sqrt(epsilon*mu)
|
24 |
+
assert m2.permittivity == epsilon
|
25 |
+
assert m2.permeability == mu
|
26 |
+
# Increasing electric permittivity and magnetic permeability
|
27 |
+
# by small amount from its value in vacuum.
|
28 |
+
m3 = Medium('m3', 9.0*10**(-12)*s**4*A**2/(m**3*kg), 1.45*10**(-6)*kg*m/(A**2*s**2))
|
29 |
+
assert m3.refractive_index > m1.refractive_index
|
30 |
+
assert m3 != m1
|
31 |
+
# Decreasing electric permittivity and magnetic permeability
|
32 |
+
# by small amount from its value in vacuum.
|
33 |
+
m4 = Medium('m4', 7.0*10**(-12)*s**4*A**2/(m**3*kg), 1.15*10**(-6)*kg*m/(A**2*s**2))
|
34 |
+
assert m4.refractive_index < m1.refractive_index
|
35 |
+
m5 = Medium('m5', permittivity=710*10**(-12)*s**4*A**2/(m**3*kg), n=1.33)
|
36 |
+
assert abs(m5.intrinsic_impedance - 6.24845417765552*kg*m**2/(A**2*s**3)) \
|
37 |
+
< 1e-12*kg*m**2/(A**2*s**3)
|
38 |
+
assert abs(m5.speed - 225407863.157895*m/s) < 1e-6*m/s
|
39 |
+
assert abs(m5.refractive_index - 1.33000000000000) < 1e-12
|
40 |
+
assert abs(m5.permittivity - 7.1e-10*A**2*s**4/(kg*m**3)) \
|
41 |
+
< 1e-20*A**2*s**4/(kg*m**3)
|
42 |
+
assert abs(m5.permeability - 2.77206575232851e-8*kg*m/(A**2*s**2)) \
|
43 |
+
< 1e-20*kg*m/(A**2*s**2)
|
44 |
+
m6 = Medium('m6', None, mu, n)
|
45 |
+
assert m6.permittivity == n**2/(c**2*mu)
|
46 |
+
# test for equality of refractive indices
|
47 |
+
assert Medium('m7').refractive_index == Medium('m8', e0, u0).refractive_index
|
48 |
+
raises(ValueError, lambda:Medium('m9', e0, u0, 2))
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_polarization.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.physics.optics.polarization import (jones_vector, stokes_vector,
|
2 |
+
jones_2_stokes, linear_polarizer, phase_retarder, half_wave_retarder,
|
3 |
+
quarter_wave_retarder, transmissive_filter, reflective_filter,
|
4 |
+
mueller_matrix, polarizing_beam_splitter)
|
5 |
+
from sympy.core.numbers import (I, pi)
|
6 |
+
from sympy.core.singleton import S
|
7 |
+
from sympy.core.symbol import symbols
|
8 |
+
from sympy.functions.elementary.exponential import exp
|
9 |
+
from sympy.matrices.dense import Matrix
|
10 |
+
|
11 |
+
|
12 |
+
def test_polarization():
|
13 |
+
assert jones_vector(0, 0) == Matrix([1, 0])
|
14 |
+
assert jones_vector(pi/2, 0) == Matrix([0, 1])
|
15 |
+
#################################################################
|
16 |
+
assert stokes_vector(0, 0) == Matrix([1, 1, 0, 0])
|
17 |
+
assert stokes_vector(pi/2, 0) == Matrix([1, -1, 0, 0])
|
18 |
+
#################################################################
|
19 |
+
H = jones_vector(0, 0)
|
20 |
+
V = jones_vector(pi/2, 0)
|
21 |
+
D = jones_vector(pi/4, 0)
|
22 |
+
A = jones_vector(-pi/4, 0)
|
23 |
+
R = jones_vector(0, pi/4)
|
24 |
+
L = jones_vector(0, -pi/4)
|
25 |
+
|
26 |
+
res = [Matrix([1, 1, 0, 0]),
|
27 |
+
Matrix([1, -1, 0, 0]),
|
28 |
+
Matrix([1, 0, 1, 0]),
|
29 |
+
Matrix([1, 0, -1, 0]),
|
30 |
+
Matrix([1, 0, 0, 1]),
|
31 |
+
Matrix([1, 0, 0, -1])]
|
32 |
+
|
33 |
+
assert [jones_2_stokes(e) for e in [H, V, D, A, R, L]] == res
|
34 |
+
#################################################################
|
35 |
+
assert linear_polarizer(0) == Matrix([[1, 0], [0, 0]])
|
36 |
+
#################################################################
|
37 |
+
delta = symbols("delta", real=True)
|
38 |
+
res = Matrix([[exp(-I*delta/2), 0], [0, exp(I*delta/2)]])
|
39 |
+
assert phase_retarder(0, delta) == res
|
40 |
+
#################################################################
|
41 |
+
assert half_wave_retarder(0) == Matrix([[-I, 0], [0, I]])
|
42 |
+
#################################################################
|
43 |
+
res = Matrix([[exp(-I*pi/4), 0], [0, I*exp(-I*pi/4)]])
|
44 |
+
assert quarter_wave_retarder(0) == res
|
45 |
+
#################################################################
|
46 |
+
assert transmissive_filter(1) == Matrix([[1, 0], [0, 1]])
|
47 |
+
#################################################################
|
48 |
+
assert reflective_filter(1) == Matrix([[1, 0], [0, -1]])
|
49 |
+
|
50 |
+
res = Matrix([[S(1)/2, S(1)/2, 0, 0],
|
51 |
+
[S(1)/2, S(1)/2, 0, 0],
|
52 |
+
[0, 0, 0, 0],
|
53 |
+
[0, 0, 0, 0]])
|
54 |
+
assert mueller_matrix(linear_polarizer(0)) == res
|
55 |
+
#################################################################
|
56 |
+
res = Matrix([[1, 0, 0, 0], [0, 0, 0, -I], [0, 0, 1, 0], [0, -I, 0, 0]])
|
57 |
+
assert polarizing_beam_splitter() == res
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_utils.py
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.numbers import comp, Rational
|
2 |
+
from sympy.physics.optics.utils import (refraction_angle, fresnel_coefficients,
|
3 |
+
deviation, brewster_angle, critical_angle, lens_makers_formula,
|
4 |
+
mirror_formula, lens_formula, hyperfocal_distance,
|
5 |
+
transverse_magnification)
|
6 |
+
from sympy.physics.optics.medium import Medium
|
7 |
+
from sympy.physics.units import e0
|
8 |
+
|
9 |
+
from sympy.core.numbers import oo
|
10 |
+
from sympy.core.symbol import symbols
|
11 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
12 |
+
from sympy.matrices.dense import Matrix
|
13 |
+
from sympy.geometry.point import Point3D
|
14 |
+
from sympy.geometry.line import Ray3D
|
15 |
+
from sympy.geometry.plane import Plane
|
16 |
+
|
17 |
+
from sympy.testing.pytest import raises
|
18 |
+
|
19 |
+
|
20 |
+
ae = lambda a, b, n: comp(a, b, 10**-n)
|
21 |
+
|
22 |
+
|
23 |
+
def test_refraction_angle():
|
24 |
+
n1, n2 = symbols('n1, n2')
|
25 |
+
m1 = Medium('m1')
|
26 |
+
m2 = Medium('m2')
|
27 |
+
r1 = Ray3D(Point3D(-1, -1, 1), Point3D(0, 0, 0))
|
28 |
+
i = Matrix([1, 1, 1])
|
29 |
+
n = Matrix([0, 0, 1])
|
30 |
+
normal_ray = Ray3D(Point3D(0, 0, 0), Point3D(0, 0, 1))
|
31 |
+
P = Plane(Point3D(0, 0, 0), normal_vector=[0, 0, 1])
|
32 |
+
assert refraction_angle(r1, 1, 1, n) == Matrix([
|
33 |
+
[ 1],
|
34 |
+
[ 1],
|
35 |
+
[-1]])
|
36 |
+
assert refraction_angle([1, 1, 1], 1, 1, n) == Matrix([
|
37 |
+
[ 1],
|
38 |
+
[ 1],
|
39 |
+
[-1]])
|
40 |
+
assert refraction_angle((1, 1, 1), 1, 1, n) == Matrix([
|
41 |
+
[ 1],
|
42 |
+
[ 1],
|
43 |
+
[-1]])
|
44 |
+
assert refraction_angle(i, 1, 1, [0, 0, 1]) == Matrix([
|
45 |
+
[ 1],
|
46 |
+
[ 1],
|
47 |
+
[-1]])
|
48 |
+
assert refraction_angle(i, 1, 1, (0, 0, 1)) == Matrix([
|
49 |
+
[ 1],
|
50 |
+
[ 1],
|
51 |
+
[-1]])
|
52 |
+
assert refraction_angle(i, 1, 1, normal_ray) == Matrix([
|
53 |
+
[ 1],
|
54 |
+
[ 1],
|
55 |
+
[-1]])
|
56 |
+
assert refraction_angle(i, 1, 1, plane=P) == Matrix([
|
57 |
+
[ 1],
|
58 |
+
[ 1],
|
59 |
+
[-1]])
|
60 |
+
assert refraction_angle(r1, 1, 1, plane=P) == \
|
61 |
+
Ray3D(Point3D(0, 0, 0), Point3D(1, 1, -1))
|
62 |
+
assert refraction_angle(r1, m1, 1.33, plane=P) == \
|
63 |
+
Ray3D(Point3D(0, 0, 0), Point3D(Rational(100, 133), Rational(100, 133), -789378201649271*sqrt(3)/1000000000000000))
|
64 |
+
assert refraction_angle(r1, 1, m2, plane=P) == \
|
65 |
+
Ray3D(Point3D(0, 0, 0), Point3D(1, 1, -1))
|
66 |
+
assert refraction_angle(r1, n1, n2, plane=P) == \
|
67 |
+
Ray3D(Point3D(0, 0, 0), Point3D(n1/n2, n1/n2, -sqrt(3)*sqrt(-2*n1**2/(3*n2**2) + 1)))
|
68 |
+
assert refraction_angle(r1, 1.33, 1, plane=P) == 0 # TIR
|
69 |
+
assert refraction_angle(r1, 1, 1, normal_ray) == \
|
70 |
+
Ray3D(Point3D(0, 0, 0), direction_ratio=[1, 1, -1])
|
71 |
+
assert ae(refraction_angle(0.5, 1, 2), 0.24207, 5)
|
72 |
+
assert ae(refraction_angle(0.5, 2, 1), 1.28293, 5)
|
73 |
+
raises(ValueError, lambda: refraction_angle(r1, m1, m2, normal_ray, P))
|
74 |
+
raises(TypeError, lambda: refraction_angle(m1, m1, m2)) # can add other values for arg[0]
|
75 |
+
raises(TypeError, lambda: refraction_angle(r1, m1, m2, None, i))
|
76 |
+
raises(TypeError, lambda: refraction_angle(r1, m1, m2, m2))
|
77 |
+
|
78 |
+
|
79 |
+
def test_fresnel_coefficients():
|
80 |
+
assert all(ae(i, j, 5) for i, j in zip(
|
81 |
+
fresnel_coefficients(0.5, 1, 1.33),
|
82 |
+
[0.11163, -0.17138, 0.83581, 0.82862]))
|
83 |
+
assert all(ae(i, j, 5) for i, j in zip(
|
84 |
+
fresnel_coefficients(0.5, 1.33, 1),
|
85 |
+
[-0.07726, 0.20482, 1.22724, 1.20482]))
|
86 |
+
m1 = Medium('m1')
|
87 |
+
m2 = Medium('m2', n=2)
|
88 |
+
assert all(ae(i, j, 5) for i, j in zip(
|
89 |
+
fresnel_coefficients(0.3, m1, m2),
|
90 |
+
[0.31784, -0.34865, 0.65892, 0.65135]))
|
91 |
+
ans = [[-0.23563, -0.97184], [0.81648, -0.57738]]
|
92 |
+
got = fresnel_coefficients(0.6, m2, m1)
|
93 |
+
for i, j in zip(got, ans):
|
94 |
+
for a, b in zip(i.as_real_imag(), j):
|
95 |
+
assert ae(a, b, 5)
|
96 |
+
|
97 |
+
|
98 |
+
def test_deviation():
|
99 |
+
n1, n2 = symbols('n1, n2')
|
100 |
+
r1 = Ray3D(Point3D(-1, -1, 1), Point3D(0, 0, 0))
|
101 |
+
n = Matrix([0, 0, 1])
|
102 |
+
i = Matrix([-1, -1, -1])
|
103 |
+
normal_ray = Ray3D(Point3D(0, 0, 0), Point3D(0, 0, 1))
|
104 |
+
P = Plane(Point3D(0, 0, 0), normal_vector=[0, 0, 1])
|
105 |
+
assert deviation(r1, 1, 1, normal=n) == 0
|
106 |
+
assert deviation(r1, 1, 1, plane=P) == 0
|
107 |
+
assert deviation(r1, 1, 1.1, plane=P).evalf(3) + 0.119 < 1e-3
|
108 |
+
assert deviation(i, 1, 1.1, normal=normal_ray).evalf(3) + 0.119 < 1e-3
|
109 |
+
assert deviation(r1, 1.33, 1, plane=P) is None # TIR
|
110 |
+
assert deviation(r1, 1, 1, normal=[0, 0, 1]) == 0
|
111 |
+
assert deviation([-1, -1, -1], 1, 1, normal=[0, 0, 1]) == 0
|
112 |
+
assert ae(deviation(0.5, 1, 2), -0.25793, 5)
|
113 |
+
assert ae(deviation(0.5, 2, 1), 0.78293, 5)
|
114 |
+
|
115 |
+
|
116 |
+
def test_brewster_angle():
|
117 |
+
m1 = Medium('m1', n=1)
|
118 |
+
m2 = Medium('m2', n=1.33)
|
119 |
+
assert ae(brewster_angle(m1, m2), 0.93, 2)
|
120 |
+
m1 = Medium('m1', permittivity=e0, n=1)
|
121 |
+
m2 = Medium('m2', permittivity=e0, n=1.33)
|
122 |
+
assert ae(brewster_angle(m1, m2), 0.93, 2)
|
123 |
+
assert ae(brewster_angle(1, 1.33), 0.93, 2)
|
124 |
+
|
125 |
+
|
126 |
+
def test_critical_angle():
|
127 |
+
m1 = Medium('m1', n=1)
|
128 |
+
m2 = Medium('m2', n=1.33)
|
129 |
+
assert ae(critical_angle(m2, m1), 0.85, 2)
|
130 |
+
|
131 |
+
|
132 |
+
def test_lens_makers_formula():
|
133 |
+
n1, n2 = symbols('n1, n2')
|
134 |
+
m1 = Medium('m1', permittivity=e0, n=1)
|
135 |
+
m2 = Medium('m2', permittivity=e0, n=1.33)
|
136 |
+
assert lens_makers_formula(n1, n2, 10, -10) == 5.0*n2/(n1 - n2)
|
137 |
+
assert ae(lens_makers_formula(m1, m2, 10, -10), -20.15, 2)
|
138 |
+
assert ae(lens_makers_formula(1.33, 1, 10, -10), 15.15, 2)
|
139 |
+
|
140 |
+
|
141 |
+
def test_mirror_formula():
|
142 |
+
u, v, f = symbols('u, v, f')
|
143 |
+
assert mirror_formula(focal_length=f, u=u) == f*u/(-f + u)
|
144 |
+
assert mirror_formula(focal_length=f, v=v) == f*v/(-f + v)
|
145 |
+
assert mirror_formula(u=u, v=v) == u*v/(u + v)
|
146 |
+
assert mirror_formula(u=oo, v=v) == v
|
147 |
+
assert mirror_formula(u=oo, v=oo) is oo
|
148 |
+
assert mirror_formula(focal_length=oo, u=u) == -u
|
149 |
+
assert mirror_formula(u=u, v=oo) == u
|
150 |
+
assert mirror_formula(focal_length=oo, v=oo) is oo
|
151 |
+
assert mirror_formula(focal_length=f, v=oo) == f
|
152 |
+
assert mirror_formula(focal_length=oo, v=v) == -v
|
153 |
+
assert mirror_formula(focal_length=oo, u=oo) is oo
|
154 |
+
assert mirror_formula(focal_length=f, u=oo) == f
|
155 |
+
assert mirror_formula(focal_length=oo, u=u) == -u
|
156 |
+
raises(ValueError, lambda: mirror_formula(focal_length=f, u=u, v=v))
|
157 |
+
|
158 |
+
|
159 |
+
def test_lens_formula():
|
160 |
+
u, v, f = symbols('u, v, f')
|
161 |
+
assert lens_formula(focal_length=f, u=u) == f*u/(f + u)
|
162 |
+
assert lens_formula(focal_length=f, v=v) == f*v/(f - v)
|
163 |
+
assert lens_formula(u=u, v=v) == u*v/(u - v)
|
164 |
+
assert lens_formula(u=oo, v=v) == v
|
165 |
+
assert lens_formula(u=oo, v=oo) is oo
|
166 |
+
assert lens_formula(focal_length=oo, u=u) == u
|
167 |
+
assert lens_formula(u=u, v=oo) == -u
|
168 |
+
assert lens_formula(focal_length=oo, v=oo) is -oo
|
169 |
+
assert lens_formula(focal_length=oo, v=v) == v
|
170 |
+
assert lens_formula(focal_length=f, v=oo) == -f
|
171 |
+
assert lens_formula(focal_length=oo, u=oo) is oo
|
172 |
+
assert lens_formula(focal_length=oo, u=u) == u
|
173 |
+
assert lens_formula(focal_length=f, u=oo) == f
|
174 |
+
raises(ValueError, lambda: lens_formula(focal_length=f, u=u, v=v))
|
175 |
+
|
176 |
+
|
177 |
+
def test_hyperfocal_distance():
|
178 |
+
f, N, c = symbols('f, N, c')
|
179 |
+
assert hyperfocal_distance(f=f, N=N, c=c) == f**2/(N*c)
|
180 |
+
assert ae(hyperfocal_distance(f=0.5, N=8, c=0.0033), 9.47, 2)
|
181 |
+
|
182 |
+
|
183 |
+
def test_transverse_magnification():
|
184 |
+
si, so = symbols('si, so')
|
185 |
+
assert transverse_magnification(si, so) == -si/so
|
186 |
+
assert transverse_magnification(30, 15) == -2
|
187 |
+
|
188 |
+
|
189 |
+
def test_lens_makers_formula_thick_lens():
|
190 |
+
n1, n2 = symbols('n1, n2')
|
191 |
+
m1 = Medium('m1', permittivity=e0, n=1)
|
192 |
+
m2 = Medium('m2', permittivity=e0, n=1.33)
|
193 |
+
assert ae(lens_makers_formula(m1, m2, 10, -10, d=1), -19.82, 2)
|
194 |
+
assert lens_makers_formula(n1, n2, 1, -1, d=0.1) == n2/((2.0 - (0.1*n1 - 0.1*n2)/n1)*(n1 - n2))
|
195 |
+
|
196 |
+
|
197 |
+
def test_lens_makers_formula_plano_lens():
|
198 |
+
n1, n2 = symbols('n1, n2')
|
199 |
+
m1 = Medium('m1', permittivity=e0, n=1)
|
200 |
+
m2 = Medium('m2', permittivity=e0, n=1.33)
|
201 |
+
assert ae(lens_makers_formula(m1, m2, 10, oo), -40.30, 2)
|
202 |
+
assert lens_makers_formula(n1, n2, 10, oo) == 10.0*n2/(n1 - n2)
|
llmeval-env/lib/python3.10/site-packages/sympy/physics/optics/tests/test_waves.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sympy.core.function import (Derivative, Function)
|
2 |
+
from sympy.core.numbers import (I, pi)
|
3 |
+
from sympy.core.symbol import (Symbol, symbols)
|
4 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
5 |
+
from sympy.functions.elementary.trigonometric import (atan2, cos, sin)
|
6 |
+
from sympy.simplify.simplify import simplify
|
7 |
+
from sympy.abc import epsilon, mu
|
8 |
+
from sympy.functions.elementary.exponential import exp
|
9 |
+
from sympy.physics.units import speed_of_light, m, s
|
10 |
+
from sympy.physics.optics import TWave
|
11 |
+
|
12 |
+
from sympy.testing.pytest import raises
|
13 |
+
|
14 |
+
c = speed_of_light.convert_to(m/s)
|
15 |
+
|
16 |
+
def test_twave():
|
17 |
+
A1, phi1, A2, phi2, f = symbols('A1, phi1, A2, phi2, f')
|
18 |
+
n = Symbol('n') # Refractive index
|
19 |
+
t = Symbol('t') # Time
|
20 |
+
x = Symbol('x') # Spatial variable
|
21 |
+
E = Function('E')
|
22 |
+
w1 = TWave(A1, f, phi1)
|
23 |
+
w2 = TWave(A2, f, phi2)
|
24 |
+
assert w1.amplitude == A1
|
25 |
+
assert w1.frequency == f
|
26 |
+
assert w1.phase == phi1
|
27 |
+
assert w1.wavelength == c/(f*n)
|
28 |
+
assert w1.time_period == 1/f
|
29 |
+
assert w1.angular_velocity == 2*pi*f
|
30 |
+
assert w1.wavenumber == 2*pi*f*n/c
|
31 |
+
assert w1.speed == c/n
|
32 |
+
|
33 |
+
w3 = w1 + w2
|
34 |
+
assert w3.amplitude == sqrt(A1**2 + 2*A1*A2*cos(phi1 - phi2) + A2**2)
|
35 |
+
assert w3.frequency == f
|
36 |
+
assert w3.phase == atan2(A1*sin(phi1) + A2*sin(phi2), A1*cos(phi1) + A2*cos(phi2))
|
37 |
+
assert w3.wavelength == c/(f*n)
|
38 |
+
assert w3.time_period == 1/f
|
39 |
+
assert w3.angular_velocity == 2*pi*f
|
40 |
+
assert w3.wavenumber == 2*pi*f*n/c
|
41 |
+
assert w3.speed == c/n
|
42 |
+
assert simplify(w3.rewrite(sin) - w2.rewrite(sin) - w1.rewrite(sin)) == 0
|
43 |
+
assert w3.rewrite('pde') == epsilon*mu*Derivative(E(x, t), t, t) + Derivative(E(x, t), x, x)
|
44 |
+
assert w3.rewrite(cos) == sqrt(A1**2 + 2*A1*A2*cos(phi1 - phi2)
|
45 |
+
+ A2**2)*cos(pi*f*n*x*s/(149896229*m) - 2*pi*f*t + atan2(A1*sin(phi1)
|
46 |
+
+ A2*sin(phi2), A1*cos(phi1) + A2*cos(phi2)))
|
47 |
+
assert w3.rewrite(exp) == sqrt(A1**2 + 2*A1*A2*cos(phi1 - phi2)
|
48 |
+
+ A2**2)*exp(I*(-2*pi*f*t + atan2(A1*sin(phi1) + A2*sin(phi2), A1*cos(phi1)
|
49 |
+
+ A2*cos(phi2)) + pi*s*f*n*x/(149896229*m)))
|
50 |
+
|
51 |
+
w4 = TWave(A1, None, 0, 1/f)
|
52 |
+
assert w4.frequency == f
|
53 |
+
|
54 |
+
w5 = w1 - w2
|
55 |
+
assert w5.amplitude == sqrt(A1**2 - 2*A1*A2*cos(phi1 - phi2) + A2**2)
|
56 |
+
assert w5.frequency == f
|
57 |
+
assert w5.phase == atan2(A1*sin(phi1) - A2*sin(phi2), A1*cos(phi1) - A2*cos(phi2))
|
58 |
+
assert w5.wavelength == c/(f*n)
|
59 |
+
assert w5.time_period == 1/f
|
60 |
+
assert w5.angular_velocity == 2*pi*f
|
61 |
+
assert w5.wavenumber == 2*pi*f*n/c
|
62 |
+
assert w5.speed == c/n
|
63 |
+
assert simplify(w5.rewrite(sin) - w1.rewrite(sin) + w2.rewrite(sin)) == 0
|
64 |
+
assert w5.rewrite('pde') == epsilon*mu*Derivative(E(x, t), t, t) + Derivative(E(x, t), x, x)
|
65 |
+
assert w5.rewrite(cos) == sqrt(A1**2 - 2*A1*A2*cos(phi1 - phi2)
|
66 |
+
+ A2**2)*cos(-2*pi*f*t + atan2(A1*sin(phi1) - A2*sin(phi2), A1*cos(phi1)
|
67 |
+
- A2*cos(phi2)) + pi*s*f*n*x/(149896229*m))
|
68 |
+
assert w5.rewrite(exp) == sqrt(A1**2 - 2*A1*A2*cos(phi1 - phi2)
|
69 |
+
+ A2**2)*exp(I*(-2*pi*f*t + atan2(A1*sin(phi1) - A2*sin(phi2), A1*cos(phi1)
|
70 |
+
- A2*cos(phi2)) + pi*s*f*n*x/(149896229*m)))
|
71 |
+
|
72 |
+
w6 = 2*w1
|
73 |
+
assert w6.amplitude == 2*A1
|
74 |
+
assert w6.frequency == f
|
75 |
+
assert w6.phase == phi1
|
76 |
+
w7 = -w6
|
77 |
+
assert w7.amplitude == -2*A1
|
78 |
+
assert w7.frequency == f
|
79 |
+
assert w7.phase == phi1
|
80 |
+
|
81 |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ula
|
12 |
+
* hyperfocal_distance
|
13 |
+
* transverse_magnification
|
14 |
+
"""
|
15 |
+
|
16 |
+
__all__ = ['refraction_angle',
|
17 |
+
'deviation',
|
18 |
+
'fresnel_coefficients',
|
19 |
+
'brewster_angle',
|
20 |
+
'critical_angle',
|
21 |
+
'lens_makers_formula',
|
22 |
+
'mirror_formula',
|
23 |
+
'lens_formula',
|
24 |
+
'hyperfocal_distance',
|
25 |
+
'transverse_magnification'
|
26 |
+
]
|
27 |
+
|
28 |
+
from sympy.core.numbers import (Float, I, oo, pi, zoo)
|
29 |
+
from sympy.core.singleton import S
|
30 |
+
from sympy.core.symbol import Symbol
|
31 |
+
from sympy.core.sympify import sympify
|
32 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
33 |
+
from sympy.functions.elementary.trigonometric import (acos, asin, atan2, cos, sin, tan)
|
34 |
+
from sympy.matrices.dense import Matrix
|
35 |
+
from sympy.polys.polytools import cancel
|
36 |
+
from sympy.series.limits import Limit
|
37 |
+
from sympy.geometry.line import Ray3D
|
38 |
+
from sympy.geometry.util import intersection
|
39 |
+
from sympy.geometry.plane import Plane
|
40 |
+
from sympy.utilities.iterables import is_sequence
|
41 |
+
from .medium import Medium
|
42 |
+
|
43 |
+
|
44 |
+
def refractive_index_of_medium(medium):
|
45 |
+
"""
|
46 |
+
Helper function that returns refractive index, given a medium
|
47 |
+
"""
|
48 |
+
if isinstance(medium, Medium):
|
49 |
+
n = medium.refractive_index
|
50 |
+
else:
|
51 |
+
n = sympify(medium)
|
52 |
+
return n
|
53 |
+
|
54 |
+
|
55 |
+
def refraction_angle(incident, medium1, medium2, normal=None, plane=None):
|
56 |
+
"""
|
57 |
+
This function calculates transmitted vector after refraction at planar
|
58 |
+
surface. ``medium1`` and ``medium2`` can be ``Medium`` or any sympifiable object.
|
59 |
+
If ``incident`` is a number then treated as angle of incidence (in radians)
|
60 |
+
in which case refraction angle is returned.
|
61 |
+
|
62 |
+
If ``incident`` is an object of `Ray3D`, `normal` also has to be an instance
|
63 |
+
of `Ray3D` in order to get the output as a `Ray3D`. Please note that if
|
64 |
+
plane of separation is not provided and normal is an instance of `Ray3D`,
|
65 |
+
``normal`` will be assumed to be intersecting incident ray at the plane of
|
66 |
+
separation. This will not be the case when `normal` is a `Matrix` or
|
67 |
+
any other sequence.
|
68 |
+
If ``incident`` is an instance of `Ray3D` and `plane` has not been provided
|
69 |
+
and ``normal`` is not `Ray3D`, output will be a `Matrix`.
|
70 |
+
|
71 |
+
Parameters
|
72 |
+
==========
|
73 |
+
|
74 |
+
incident : Matrix, Ray3D, sequence or a number
|
75 |
+
Incident vector or angle of incidence
|
76 |
+
medium1 : sympy.physics.optics.medium.Medium or sympifiable
|
77 |
+
Medium 1 or its refractive index
|
78 |
+
medium2 : sympy.physics.optics.medium.Medium or sympifiable
|
79 |
+
Medium 2 or its refractive index
|
80 |
+
normal : Matrix, Ray3D, or sequence
|
81 |
+
Normal vector
|
82 |
+
plane : Plane
|
83 |
+
Plane of separation of the two media.
|
84 |
+
|
85 |
+
Returns
|
86 |
+
=======
|
87 |
+
|
88 |
+
Returns an angle of refraction or a refracted ray depending on inputs.
|
89 |
+
|
90 |
+
Examples
|
91 |
+
========
|
92 |
+
|
93 |
+
>>> from sympy.physics.optics import refraction_angle
|
94 |
+
>>> from sympy.geometry import Point3D, Ray3D, Plane
|
95 |
+
>>> from sympy.matrices import Matrix
|
96 |
+
>>> from sympy import symbols, pi
|
97 |
+
>>> n = Matrix([0, 0, 1])
|
98 |
+
>>> P = Plane(Point3D(0, 0, 0), normal_vector=[0, 0, 1])
|
99 |
+
>>> r1 = Ray3D(Point3D(-1, -1, 1), Point3D(0, 0, 0))
|
100 |
+
>>> refraction_angle(r1, 1, 1, n)
|
101 |
+
Matrix([
|
102 |
+
[ 1],
|
103 |
+
[ 1],
|
104 |
+
[-1]])
|
105 |
+
>>> refraction_angle(r1, 1, 1, plane=P)
|
106 |
+
Ray3D(Point3D(0, 0, 0), Point3D(1, 1, -1))
|
107 |
+
|
108 |
+
With different index of refraction of the two media
|
109 |
+
|
110 |
+
>>> n1, n2 = symbols('n1, n2')
|
111 |
+
>>> refraction_angle(r1, n1, n2, n)
|
112 |
+
Matrix([
|
113 |
+
[ n1/n2],
|
114 |
+
[ n1/n2],
|
115 |
+
[-sqrt(3)*sqrt(-2*n1**2/(3*n2**2) + 1)]])
|
116 |
+
>>> refraction_angle(r1, n1, n2, plane=P)
|
117 |
+
Ray3D(Point3D(0, 0, 0), Point3D(n1/n2, n1/n2, -sqrt(3)*sqrt(-2*n1**2/(3*n2**2) + 1)))
|
118 |
+
>>> round(refraction_angle(pi/6, 1.2, 1.5), 5)
|
119 |
+
0.41152
|
120 |
+
"""
|
121 |
+
|
122 |
+
n1 = refractive_index_of_medium(medium1)
|
123 |
+
n2 = refractive_index_of_medium(medium2)
|
124 |
+
|
125 |
+
# check if an incidence angle was supplied instead of a ray
|
126 |
+
try:
|
127 |
+
angle_of_incidence = float(incident)
|
128 |
+
except TypeError:
|
129 |
+
angle_of_incidence = None
|
130 |
+
|
131 |
+
try:
|
132 |
+
critical_angle_ = critical_angle(medium1, medium2)
|
133 |
+
except (ValueError, TypeError):
|
134 |
+
critical_angle_ = None
|
135 |
+
|
136 |
+
if angle_of_incidence is not None:
|
137 |
+
if normal is not None or plane is not None:
|
138 |
+
raise ValueError('Normal/plane not allowed if incident is an angle')
|
139 |
+
|
140 |
+
if not 0.0 <= angle_of_incidence < pi*0.5:
|
141 |
+
raise ValueError('Angle of incidence not in range [0:pi/2)')
|
142 |
+
|
143 |
+
if critical_angle_ and angle_of_incidence > critical_angle_:
|
144 |
+
raise ValueError('Ray undergoes total internal reflection')
|
145 |
+
return asin(n1*sin(angle_of_incidence)/n2)
|
146 |
+
|
147 |
+
# Treat the incident as ray below
|
148 |
+
# A flag to check whether to return Ray3D or not
|
149 |
+
return_ray = False
|
150 |
+
|
151 |
+
if plane is not None and normal is not None:
|
152 |
+
raise ValueError("Either plane or normal is acceptable.")
|
153 |
+
|
154 |
+
if not isinstance(incident, Matrix):
|
155 |
+
if is_sequence(incident):
|
156 |
+
_incident = Matrix(incident)
|
157 |
+
elif isinstance(incident, Ray3D):
|
158 |
+
_incident = Matrix(incident.direction_ratio)
|
159 |
+
else:
|
160 |
+
raise TypeError(
|
161 |
+
"incident should be a Matrix, Ray3D, or sequence")
|
162 |
+
else:
|
163 |
+
_incident = incident
|
164 |
+
|
165 |
+
# If plane is provided, get direction ratios of the normal
|
166 |
+
# to the plane from the plane else go with `normal` param.
|
167 |
+
if plane is not None:
|
168 |
+
if not isinstance(plane, Plane):
|
169 |
+
raise TypeError("plane should be an instance of geometry.plane.Plane")
|
170 |
+
# If we have the plane, we can get the intersection
|
171 |
+
# point of incident ray and the plane and thus return
|
172 |
+
# an instance of Ray3D.
|
173 |
+
if isinstance(incident, Ray3D):
|
174 |
+
return_ray = True
|
175 |
+
intersection_pt = plane.intersection(incident)[0]
|
176 |
+
_normal = Matrix(plane.normal_vector)
|
177 |
+
else:
|
178 |
+
if not isinstance(normal, Matrix):
|
179 |
+
if is_sequence(normal):
|
180 |
+
_normal = Matrix(normal)
|
181 |
+
elif isinstance(normal, Ray3D):
|
182 |
+
_normal = Matrix(normal.direction_ratio)
|
183 |
+
if isinstance(incident, Ray3D):
|
184 |
+
intersection_pt = intersection(incident, normal)
|
185 |
+
if len(intersection_pt) == 0:
|
186 |
+
raise ValueError(
|
187 |
+
"Normal isn't concurrent with the incident ray.")
|
188 |
+
else:
|
189 |
+
return_ray = True
|
190 |
+
intersection_pt = intersection_pt[0]
|
191 |
+
else:
|
192 |
+
raise TypeError(
|
193 |
+
"Normal should be a Matrix, Ray3D, or sequence")
|
194 |
+
else:
|
195 |
+
_normal = normal
|
196 |
+
|
197 |
+
eta = n1/n2 # Relative index of refraction
|
198 |
+
# Calculating magnitude of the vectors
|
199 |
+
mag_incident = sqrt(sum([i**2 for i in _incident]))
|
200 |
+
mag_normal = sqrt(sum([i**2 for i in _normal]))
|
201 |
+
# Converting vectors to unit vectors by dividing
|
202 |
+
# them with their magnitudes
|
203 |
+
_incident /= mag_incident
|
204 |
+
_normal /= mag_normal
|
205 |
+
c1 = -_incident.dot(_normal) # cos(angle_of_incidence)
|
206 |
+
cs2 = 1 - eta**2*(1 - c1**2) # cos(angle_of_refraction)**2
|
207 |
+
if cs2.is_negative: # This is the case of total internal reflection(TIR).
|
208 |
+
return S.Zero
|
209 |
+
drs = eta*_incident + (eta*c1 - sqrt(cs2))*_normal
|
210 |
+
# Multiplying unit vector by its magnitude
|
211 |
+
drs = drs*mag_incident
|
212 |
+
if not return_ray:
|
213 |
+
return drs
|
214 |
+
else:
|
215 |
+
return Ray3D(intersection_pt, direction_ratio=drs)
|
216 |
+
|
217 |
+
|
218 |
+
def fresnel_coefficients(angle_of_incidence, medium1, medium2):
|
219 |
+
"""
|
220 |
+
This function uses Fresnel equations to calculate reflection and
|
221 |
+
transmission coefficients. Those are obtained for both polarisations
|
222 |
+
when the electric field vector is in the plane of incidence (labelled 'p')
|
223 |
+
and when the electric field vector is perpendicular to the plane of
|
224 |
+
incidence (labelled 's'). There are four real coefficients unless the
|
225 |
+
incident ray reflects in total internal in which case there are two complex
|
226 |
+
ones. Angle of incidence is the angle between the incident ray and the
|
227 |
+
surface normal. ``medium1`` and ``medium2`` can be ``Medium`` or any
|
228 |
+
sympifiable object.
|
229 |
+
|
230 |
+
Parameters
|
231 |
+
==========
|
232 |
+
|
233 |
+
angle_of_incidence : sympifiable
|
234 |
+
|
235 |
+
medium1 : Medium or sympifiable
|
236 |
+
Medium 1 or its refractive index
|
237 |
+
|
238 |
+
medium2 : Medium or sympifiable
|
239 |
+
Medium 2 or its refractive index
|
240 |
+
|
241 |
+
Returns
|
242 |
+
=======
|
243 |
+
|
244 |
+
Returns a list with four real Fresnel coefficients:
|
245 |
+
[reflection p (TM), reflection s (TE),
|
246 |
+
transmission p (TM), transmission s (TE)]
|
247 |
+
If the ray is undergoes total internal reflection then returns a
|
248 |
+
list of two complex Fresnel coefficients:
|
249 |
+
[reflection p (TM), reflection s (TE)]
|
250 |
+
|
251 |
+
Examples
|
252 |
+
========
|
253 |
+
|
254 |
+
>>> from sympy.physics.optics import fresnel_coefficients
|
255 |
+
>>> fresnel_coefficients(0.3, 1, 2)
|
256 |
+
[0.317843553417859, -0.348645229818821,
|
257 |
+
0.658921776708929, 0.651354770181179]
|
258 |
+
>>> fresnel_coefficients(0.6, 2, 1)
|
259 |
+
[-0.235625382192159 - 0.971843958291041*I,
|
260 |
+
0.816477005968898 - 0.577377951366403*I]
|
261 |
+
|
262 |
+
References
|
263 |
+
==========
|
264 |
+
|
265 |
+
.. [1] https://en.wikipedia.org/wiki/Fresnel_equations
|
266 |
+
"""
|
267 |
+
if not 0 <= 2*angle_of_incidence < pi:
|
268 |
+
raise ValueError('Angle of incidence not in range [0:pi/2)')
|
269 |
+
|
270 |
+
n1 = refractive_index_of_medium(medium1)
|
271 |
+
n2 = refractive_index_of_medium(medium2)
|
272 |
+
|
273 |
+
angle_of_refraction = asin(n1*sin(angle_of_incidence)/n2)
|
274 |
+
try:
|
275 |
+
angle_of_total_internal_reflection_onset = critical_angle(n1, n2)
|
276 |
+
except ValueError:
|
277 |
+
angle_of_total_internal_reflection_onset = None
|
278 |
+
|
279 |
+
if angle_of_total_internal_reflection_onset is None or\
|
280 |
+
angle_of_total_internal_reflection_onset > angle_of_incidence:
|
281 |
+
R_s = -sin(angle_of_incidence - angle_of_refraction)\
|
282 |
+
/sin(angle_of_incidence + angle_of_refraction)
|
283 |
+
R_p = tan(angle_of_incidence - angle_of_refraction)\
|
284 |
+
/tan(angle_of_incidence + angle_of_refraction)
|
285 |
+
T_s = 2*sin(angle_of_refraction)*cos(angle_of_incidence)\
|
286 |
+
/sin(angle_of_incidence + angle_of_refraction)
|
287 |
+
T_p = 2*sin(angle_of_refraction)*cos(angle_of_incidence)\
|
288 |
+
/(sin(angle_of_incidence + angle_of_refraction)\
|
289 |
+
*cos(angle_of_incidence - angle_of_refraction))
|
290 |
+
return [R_p, R_s, T_p, T_s]
|
291 |
+
else:
|
292 |
+
n = n2/n1
|
293 |
+
R_s = cancel((cos(angle_of_incidence)-\
|
294 |
+
I*sqrt(sin(angle_of_incidence)**2 - n**2))\
|
295 |
+
/(cos(angle_of_incidence)+\
|
296 |
+
I*sqrt(sin(angle_of_incidence)**2 - n**2)))
|
297 |
+
R_p = cancel((n**2*cos(angle_of_incidence)-\
|
298 |
+
I*sqrt(sin(angle_of_incidence)**2 - n**2))\
|
299 |
+
/(n**2*cos(angle_of_incidence)+\
|
300 |
+
I*sqrt(sin(angle_of_incidence)**2 - n**2)))
|
301 |
+
return [R_p, R_s]
|
302 |
+
|
303 |
+
|
304 |
+
def deviation(incident, medium1, medium2, normal=None, plane=None):
|
305 |
+
"""
|
306 |
+
This function calculates the angle of deviation of a ray
|
307 |
+
due to refraction at planar surface.
|
308 |
+
|
309 |
+
Parameters
|
310 |
+
==========
|
311 |
+
|
312 |
+
incident : Matrix, Ray3D, sequence or float
|
313 |
+
Incident vector or angle of incidence
|
314 |
+
medium1 : sympy.physics.optics.medium.Medium or sympifiable
|
315 |
+
Medium 1 or its refractive index
|
316 |
+
medium2 : sympy.physics.optics.medium.Medium or sympifiable
|
317 |
+
Medium 2 or its refractive index
|
318 |
+
normal : Matrix, Ray3D, or sequence
|
319 |
+
Normal vector
|
320 |
+
plane : Plane
|
321 |
+
Plane of separation of the two media.
|
322 |
+
|
323 |
+
Returns angular deviation between incident and refracted rays
|
324 |
+
|
325 |
+
Examples
|
326 |
+
========
|
327 |
+
|
328 |
+
>>> from sympy.physics.optics import deviation
|
329 |
+
>>> from sympy.geometry import Point3D, Ray3D, Plane
|
330 |
+
>>> from sympy.matrices import Matrix
|
331 |
+
>>> from sympy import symbols
|
332 |
+
>>> n1, n2 = symbols('n1, n2')
|
333 |
+
>>> n = Matrix([0, 0, 1])
|
334 |
+
>>> P = Plane(Point3D(0, 0, 0), normal_vector=[0, 0, 1])
|
335 |
+
>>> r1 = Ray3D(Point3D(-1, -1, 1), Point3D(0, 0, 0))
|
336 |
+
>>> deviation(r1, 1, 1, n)
|
337 |
+
0
|
338 |
+
>>> deviation(r1, n1, n2, plane=P)
|
339 |
+
-acos(-sqrt(-2*n1**2/(3*n2**2) + 1)) + acos(-sqrt(3)/3)
|
340 |
+
>>> round(deviation(0.1, 1.2, 1.5), 5)
|
341 |
+
-0.02005
|
342 |
+
"""
|
343 |
+
refracted = refraction_angle(incident,
|
344 |
+
medium1,
|
345 |
+
medium2,
|
346 |
+
normal=normal,
|
347 |
+
plane=plane)
|
348 |
+
try:
|
349 |
+
angle_of_incidence = Float(incident)
|
350 |
+
except TypeError:
|
351 |
+
angle_of_incidence = None
|
352 |
+
|
353 |
+
if angle_of_incidence is not None:
|
354 |
+
return float(refracted) - angle_of_incidence
|
355 |
+
|
356 |
+
if refracted != 0:
|
357 |
+
if isinstance(refracted, Ray3D):
|
358 |
+
refracted = Matrix(refracted.direction_ratio)
|
359 |
+
|
360 |
+
if not isinstance(incident, Matrix):
|
361 |
+
if is_sequence(incident):
|
362 |
+
_incident = Matrix(incident)
|
363 |
+
elif isinstance(incident, Ray3D):
|
364 |
+
_incident = Matrix(incident.direction_ratio)
|
365 |
+
else:
|
366 |
+
raise TypeError(
|
367 |
+
"incident should be a Matrix, Ray3D, or sequence")
|
368 |
+
else:
|
369 |
+
_incident = incident
|
370 |
+
|
371 |
+
if plane is None:
|
372 |
+
if not isinstance(normal, Matrix):
|
373 |
+
if is_sequence(normal):
|
374 |
+
_normal = Matrix(normal)
|
375 |
+
elif isinstance(normal, Ray3D):
|
376 |
+
_normal = Matrix(normal.direction_ratio)
|
377 |
+
else:
|
378 |
+
raise TypeError(
|
379 |
+
"normal should be a Matrix, Ray3D, or sequence")
|
380 |
+
else:
|
381 |
+
_normal = normal
|
382 |
+
else:
|
383 |
+
_normal = Matrix(plane.normal_vector)
|
384 |
+
|
385 |
+
mag_incident = sqrt(sum([i**2 for i in _incident]))
|
386 |
+
mag_normal = sqrt(sum([i**2 for i in _normal]))
|
387 |
+
mag_refracted = sqrt(sum([i**2 for i in refracted]))
|
388 |
+
_incident /= mag_incident
|
389 |
+
_normal /= mag_normal
|
390 |
+
refracted /= mag_refracted
|
391 |
+
i = acos(_incident.dot(_normal))
|
392 |
+
r = acos(refracted.dot(_normal))
|
393 |
+
return i - r
|
394 |
+
|
395 |
+
|
396 |
+
def brewster_angle(medium1, medium2):
|
397 |
+
"""
|
398 |
+
This function calculates the Brewster's angle of incidence to Medium 2 from
|
399 |
+
Medium 1 in radians.
|
400 |
+
|
401 |
+
Parameters
|
402 |
+
==========
|
403 |
+
|
404 |
+
medium 1 : Medium or sympifiable
|
405 |
+
Refractive index of Medium 1
|
406 |
+
medium 2 : Medium or sympifiable
|
407 |
+
Refractive index of Medium 1
|
408 |
+
|
409 |
+
Examples
|
410 |
+
========
|
411 |
+
|
412 |
+
>>> from sympy.physics.optics import brewster_angle
|
413 |
+
>>> brewster_angle(1, 1.33)
|
414 |
+
0.926093295503462
|
415 |
+
|
416 |
+
"""
|
417 |
+
|
418 |
+
n1 = refractive_index_of_medium(medium1)
|
419 |
+
n2 = refractive_index_of_medium(medium2)
|
420 |
+
|
421 |
+
return atan2(n2, n1)
|
422 |
+
|
423 |
+
def critical_angle(medium1, medium2):
|
424 |
+
"""
|
425 |
+
This function calculates the critical angle of incidence (marking the onset
|
426 |
+
of total internal) to Medium 2 from Medium 1 in radians.
|
427 |
+
|
428 |
+
Parameters
|
429 |
+
==========
|
430 |
+
|
431 |
+
medium 1 : Medium or sympifiable
|
432 |
+
Refractive index of Medium 1.
|
433 |
+
medium 2 : Medium or sympifiable
|
434 |
+
Refractive index of Medium 1.
|
435 |
+
|
436 |
+
Examples
|
437 |
+
========
|
438 |
+
|
439 |
+
>>> from sympy.physics.optics import critical_angle
|
440 |
+
>>> critical_angle(1.33, 1)
|
441 |
+
0.850908514477849
|
442 |
+
|
443 |
+
"""
|
444 |
+
|
445 |
+
n1 = refractive_index_of_medium(medium1)
|
446 |
+
n2 = refractive_index_of_medium(medium2)
|
447 |
+
|
448 |
+
if n2 > n1:
|
449 |
+
raise ValueError('Total internal reflection impossible for n1 < n2')
|
450 |
+
else:
|
451 |
+
return asin(n2/n1)
|
452 |
+
|
453 |
+
|
454 |
+
|
455 |
+
def lens_makers_formula(n_lens, n_surr, r1, r2, d=0):
|
456 |
+
"""
|
457 |
+
This function calculates focal length of a lens.
|
458 |
+
It follows cartesian sign convention.
|
459 |
+
|
460 |
+
Parameters
|
461 |
+
==========
|
462 |
+
|
463 |
+
n_lens : Medium or sympifiable
|
464 |
+
Index of refraction of lens.
|
465 |
+
n_surr : Medium or sympifiable
|
466 |
+
Index of reflection of surrounding.
|
467 |
+
r1 : sympifiable
|
468 |
+
Radius of curvature of first surface.
|
469 |
+
r2 : sympifiable
|
470 |
+
Radius of curvature of second surface.
|
471 |
+
d : sympifiable, optional
|
472 |
+
Thickness of lens, default value is 0.
|
473 |
+
|
474 |
+
Examples
|
475 |
+
========
|
476 |
+
|
477 |
+
>>> from sympy.physics.optics import lens_makers_formula
|
478 |
+
>>> from sympy import S
|
479 |
+
>>> lens_makers_formula(1.33, 1, 10, -10)
|
480 |
+
15.1515151515151
|
481 |
+
>>> lens_makers_formula(1.2, 1, 10, S.Infinity)
|
482 |
+
50.0000000000000
|
483 |
+
>>> lens_makers_formula(1.33, 1, 10, -10, d=1)
|
484 |
+
15.3418463277618
|
485 |
+
|
486 |
+
"""
|
487 |
+
|
488 |
+
if isinstance(n_lens, Medium):
|
489 |
+
n_lens = n_lens.refractive_index
|
490 |
+
else:
|
491 |
+
n_lens = sympify(n_lens)
|
492 |
+
if isinstance(n_surr, Medium):
|
493 |
+
n_surr = n_surr.refractive_index
|
494 |
+
else:
|
495 |
+
n_surr = sympify(n_surr)
|
496 |
+
d = sympify(d)
|
497 |
+
|
498 |
+
focal_length = 1/((n_lens - n_surr) / n_surr*(1/r1 - 1/r2 + (((n_lens - n_surr) * d) / (n_lens * r1 * r2))))
|
499 |
+
|
500 |
+
if focal_length == zoo:
|
501 |
+
return S.Infinity
|
502 |
+
return focal_length
|
503 |
+
|
504 |
+
|
505 |
+
def mirror_formula(focal_length=None, u=None, v=None):
|
506 |
+
"""
|
507 |
+
This function provides one of the three parameters
|
508 |
+
when two of them are supplied.
|
509 |
+
This is valid only for paraxial rays.
|
510 |
+
|
511 |
+
Parameters
|
512 |
+
==========
|
513 |
+
|
514 |
+
focal_length : sympifiable
|
515 |
+
Focal length of the mirror.
|
516 |
+
u : sympifiable
|
517 |
+
Distance of object from the pole on
|
518 |
+
the principal axis.
|
519 |
+
v : sympifiable
|
520 |
+
Distance of the image from the pole
|
521 |
+
on the principal axis.
|
522 |
+
|
523 |
+
Examples
|
524 |
+
========
|
525 |
+
|
526 |
+
>>> from sympy.physics.optics import mirror_formula
|
527 |
+
>>> from sympy.abc import f, u, v
|
528 |
+
>>> mirror_formula(focal_length=f, u=u)
|
529 |
+
f*u/(-f + u)
|
530 |
+
>>> mirror_formula(focal_length=f, v=v)
|
531 |
+
f*v/(-f + v)
|
532 |
+
>>> mirror_formula(u=u, v=v)
|
533 |
+
u*v/(u + v)
|
534 |
+
|
535 |
+
"""
|
536 |
+
if focal_length and u and v:
|
537 |
+
raise ValueError("Please provide only two parameters")
|
538 |
+
|
539 |
+
focal_length = sympify(focal_length)
|
540 |
+
u = sympify(u)
|
541 |
+
v = sympify(v)
|
542 |
+
if u is oo:
|
543 |
+
_u = Symbol('u')
|
544 |
+
if v is oo:
|
545 |
+
_v = Symbol('v')
|
546 |
+
if focal_length is oo:
|
547 |
+
_f = Symbol('f')
|
548 |
+
if focal_length is None:
|
549 |
+
if u is oo and v is oo:
|
550 |
+
return Limit(Limit(_v*_u/(_v + _u), _u, oo), _v, oo).doit()
|
551 |
+
if u is oo:
|
552 |
+
return Limit(v*_u/(v + _u), _u, oo).doit()
|
553 |
+
if v is oo:
|
554 |
+
return Limit(_v*u/(_v + u), _v, oo).doit()
|
555 |
+
return v*u/(v + u)
|
556 |
+
if u is None:
|
557 |
+
if v is oo and focal_length is oo:
|
558 |
+
return Limit(Limit(_v*_f/(_v - _f), _v, oo), _f, oo).doit()
|
559 |
+
if v is oo:
|
560 |
+
return Limit(_v*focal_length/(_v - focal_length), _v, oo).doit()
|
561 |
+
if focal_length is oo:
|
562 |
+
return Limit(v*_f/(v - _f), _f, oo).doit()
|
563 |
+
return v*focal_length/(v - focal_length)
|
564 |
+
if v is None:
|
565 |
+
if u is oo and focal_length is oo:
|
566 |
+
return Limit(Limit(_u*_f/(_u - _f), _u, oo), _f, oo).doit()
|
567 |
+
if u is oo:
|
568 |
+
return Limit(_u*focal_length/(_u - focal_length), _u, oo).doit()
|
569 |
+
if focal_length is oo:
|
570 |
+
return Limit(u*_f/(u - _f), _f, oo).doit()
|
571 |
+
return u*focal_length/(u - focal_length)
|
572 |
+
|
573 |
+
|
574 |
+
def lens_formula(focal_length=None, u=None, v=None):
|
575 |
+
"""
|
576 |
+
This function provides one of the three parameters
|
577 |
+
when two of them are supplied.
|
578 |
+
This is valid only for paraxial rays.
|
579 |
+
|
580 |
+
Parameters
|
581 |
+
==========
|
582 |
+
|
583 |
+
focal_length : sympifiable
|
584 |
+
Focal length of the mirror.
|
585 |
+
u : sympifiable
|
586 |
+
Distance of object from the optical center on
|
587 |
+
the principal axis.
|
588 |
+
v : sympifiable
|
589 |
+
Distance of the image from the optical center
|
590 |
+
on the principal axis.
|
591 |
+
|
592 |
+
Examples
|
593 |
+
========
|
594 |
+
|
595 |
+
>>> from sympy.physics.optics import lens_formula
|
596 |
+
>>> from sympy.abc import f, u, v
|
597 |
+
>>> lens_formula(focal_length=f, u=u)
|
598 |
+
f*u/(f + u)
|
599 |
+
>>> lens_formula(focal_length=f, v=v)
|
600 |
+
f*v/(f - v)
|
601 |
+
>>> lens_formula(u=u, v=v)
|
602 |
+
u*v/(u - v)
|
603 |
+
|
604 |
+
"""
|
605 |
+
if focal_length and u and v:
|
606 |
+
raise ValueError("Please provide only two parameters")
|
607 |
+
|
608 |
+
focal_length = sympify(focal_length)
|
609 |
+
u = sympify(u)
|
610 |
+
v = sympify(v)
|
611 |
+
if u is oo:
|
612 |
+
_u = Symbol('u')
|
613 |
+
if v is oo:
|
614 |
+
_v = Symbol('v')
|
615 |
+
if focal_length is oo:
|
616 |
+
_f = Symbol('f')
|
617 |
+
if focal_length is None:
|
618 |
+
if u is oo and v is oo:
|
619 |
+
return Limit(Limit(_v*_u/(_u - _v), _u, oo), _v, oo).doit()
|
620 |
+
if u is oo:
|
621 |
+
return Limit(v*_u/(_u - v), _u, oo).doit()
|
622 |
+
if v is oo:
|
623 |
+
return Limit(_v*u/(u - _v), _v, oo).doit()
|
624 |
+
return v*u/(u - v)
|
625 |
+
if u is None:
|
626 |
+
if v is oo and focal_length is oo:
|
627 |
+
return Limit(Limit(_v*_f/(_f - _v), _v, oo), _f, oo).doit()
|
628 |
+
if v is oo:
|
629 |
+
return Limit(_v*focal_length/(focal_length - _v), _v, oo).doit()
|
630 |
+
if focal_length is oo:
|
631 |
+
return Limit(v*_f/(_f - v), _f, oo).doit()
|
632 |
+
return v*focal_length/(focal_length - v)
|
633 |
+
if v is None:
|
634 |
+
if u is oo and focal_length is oo:
|
635 |
+
return Limit(Limit(_u*_f/(_u + _f), _u, oo), _f, oo).doit()
|
636 |
+
if u is oo:
|
637 |
+
return Limit(_u*focal_length/(_u + focal_length), _u, oo).doit()
|
638 |
+
if focal_length is oo:
|
639 |
+
return Limit(u*_f/(u + _f), _f, oo).doit()
|
640 |
+
return u*focal_length/(u + focal_length)
|
641 |
+
|
642 |
+
def hyperfocal_distance(f, N, c):
|
643 |
+
"""
|
644 |
+
|
645 |
+
Parameters
|
646 |
+
==========
|
647 |
+
|
648 |
+
f: sympifiable
|
649 |
+
Focal length of a given lens.
|
650 |
+
|
651 |
+
N: sympifiable
|
652 |
+
F-number of a given lens.
|
653 |
+
|
654 |
+
c: sympifiable
|
655 |
+
Circle of Confusion (CoC) of a given image format.
|
656 |
+
|
657 |
+
Example
|
658 |
+
=======
|
659 |
+
|
660 |
+
>>> from sympy.physics.optics import hyperfocal_distance
|
661 |
+
>>> round(hyperfocal_distance(f = 0.5, N = 8, c = 0.0033), 2)
|
662 |
+
9.47
|
663 |
+
"""
|
664 |
+
|
665 |
+
f = sympify(f)
|
666 |
+
N = sympify(N)
|
667 |
+
c = sympify(c)
|
668 |
+
|
669 |
+
return (1/(N * c))*(f**2)
|
670 |
+
|
671 |
+
def transverse_magnification(si, so):
|
672 |
+
"""
|
673 |
+
|
674 |
+
Calculates the transverse magnification, which is the ratio of the
|
675 |
+
image size to the object size.
|
676 |
+
|
677 |
+
Parameters
|
678 |
+
==========
|
679 |
+
|
680 |
+
so: sympifiable
|
681 |
+
Lens-object distance.
|
682 |
+
|
683 |
+
si: sympifiable
|
684 |
+
Lens-image distance.
|
685 |
+
|
686 |
+
Example
|
687 |
+
=======
|
688 |
+
|
689 |
+
>>> from sympy.physics.optics import transverse_magnification
|
690 |
+
>>> transverse_magnification(30, 15)
|
691 |
+
-2
|
692 |
+
|
693 |
+
"""
|
694 |
+
|
695 |
+
si = sympify(si)
|
696 |
+
so = sympify(so)
|
697 |
+
|
698 |
+
return (-(si/so))
|