14 May 2013 17:00:13 UTC
- Distribution: Math-SymbolicX-Complex
- Module version: 1.01
- Source (raw)
- Browse (raw)
- How to Contribute
- Testers (438 / 0 / 0)
- KwaliteeBus factor: 0
- 85.37% Coverage
- License: perl_5
- Activity24 month
- Download (4.61KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest versionSMUELLER Steffen Müllerand 1 contributors
- Steffen Mueller <symbolic-module at steffen-mueller dot net>
Math::SymbolicX::Complex - Complex number support for the Math::Symbolic parser
use Math::Symbolic qw/parse_from_string/; use Math::SymbolicX::Complex; my $formula = parse_from_string('3 * complex(3,2)^2 + polar(1, pi/2)'); print $formula->value(); # prints '14.9999999997949+37i' # (blame the inaccuracy on the floating point representation of "pi")
This module adds complex number support to Math::Symbolic. It does so by extending the parser of the Math::Symbolic module (that is, the one stored in $Math::Symbolic::Parser) with certain special functions that create complex constants. (Math::Symbolic::Variable objects have been able to contain complex number objects since the very beginning.)
All constants in strings that are parsed by Math::Symbolic::Parser are converted to Math::Symbolic::Constant objects holding the value associated to the constant in an ordinary Perl Scalar by default. Unfortunately, that means you are limited to real floating point numbers.
On the other hand, there's the formidable Math::Complex module that gives complex number support to Perl. Since the Math::Symbolic::Scalar objects can hold any object, you can build your trees by hand using Math::Complex objects instead of Perl Scalars for the value of the constants. But since the Math::Symbolic::Parser is by far the most convenient interface to Math::Symbolic, there had to be a reasonably simple way of introducing Math::Complex support to the parser. So here goes.
In order to complex number constants in Math::Symbolic trees from the parser, you just load this extension module and wrap any of the functions listed hereafter around any constants that are complex in nature.
The aforementioned functions are
complex(RE, IM)takes a real portion and an imaginary portion of the complex number as arguments. That means, it uses the
Math::Complex-make(RE, IM)> method to create the Math::Complex objects. Similarily,
Math::Complex-emake(R, ARG)> syntax provided by Math::Complex. (Polar notation is r*e^(i*arg). It is equivalent to the
x+i*ynotation because it also covers the whole complex plane.)
There are some usability extensions to the simple
polar(R, ARG)notations: You can use the basic operators ('+', '-', '*', '/', and '**') and the symbolic constant 'pi' in the expressions for RE, IM, R, and ARG. That means
polar(1, pi/2)should be translated to
Note, however, that the floating point representation of pi used in this module is far from exact. So, instead of yielding
0+ias a result, the above example will be
-3.49148336110938e-015+i. Of course,
-3.49148336110938e-015is as close to a real zero as you'll get, but testing for equality with the
==operator will break.
Copyright (C) 2004-2007, 2013 Steffen Mueller
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
You may contact the author at symbolic-module at steffen-mueller dot net
Please send feedback, bug reports, and support requests to the Math::Symbolic support mailing list: math-symbolic-support at lists dot sourceforge dot net. Please consider letting us know how you use Math::Symbolic. Thank you.
If you're interested in helping with the development or extending the module's functionality, please contact the developers' mailing list: math-symbolic-develop at lists dot sourceforge dot net.
New versions of this module can be found on http://steffen-mueller.net or CPAN.
You should definately be familiar with Math::Complex before you start using this module because the objects that are returned from
$formula-value()> calls are Math::Complex objects.
Refer to Math::SymbolicX::ParserExtensionFactory for the implementation details.
Module Install Instructions
To install Math::SymbolicX::Complex, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Math::SymbolicX::Complex
For more information on module installation, please visit the detailed CPAN module installation guide.