-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complex functions #34
Comments
I guess this "just" needs a simplifier. |
Yeah it sounds like it's a job for a simplification tool |
This partially works using a custom complex type: using ModelingToolkit
struct Compl{T}
re::T
im::T
end
Base.show(io::IO, x::Compl) = print(io, "($(x.re)) + i*($(x.im))")
import Base: +, -, *, real, imag, conj
+(a::Compl, b::Compl) = Compl(a.re + b.re, a.im + b.im)
*(a::Compl, b::Compl) = Compl(a.re * b.re - a.im * b.im, a.re*b.im + a.im*b.re)
conj(a::Compl) = Compl(a.re, -a.im)
@variables a b
x = Compl(a, b)
real(x::Compl) = x.re
imag(x::Compl) = x.im The simplification is the problem: julia> x = Compl(a, b)
(a) + i*(b)
julia> x * conj(x)
(a * a - b * -b) + i*(a * -b + b * a)
julia> y = x * conj(x)
(a * a - b * -b) + i*(a * -b + b * a)
julia> imag(y)
a * -b + b * a cc @shashi |
By the way, this is another example where Julia's restriction to defining |
Is this now doable with the new simplification features? @shashi |
Your custom type works if you pass the final result to |
You can now simplify the real and imaginary parts separately: julia> ex = x * conj(x)
((a * a) - (b * (-b))) + i*((a * (-b)) + (b * a))
julia> simplify(real(ex))
(a ^ 2) + (b ^ 2)
julia> simplify(imag(ex))
0 |
so I guess it is a straightforward method extension for |
This now works. julia> @variables a b
(a, b)
julia> c = a + b * im
a + (b)*im
julia> c*c*c
a*((a^2) - (b^2)) - (2a*(b^2)) + (b*((a^2) - (b^2)) + 2b*(a^2))*im |
It would be great to be able to extract real and imaginary parts of a complex function.
Currently this does not seem to work, e.g.
It needs to know that
im^2
is-1
.The text was updated successfully, but these errors were encountered: