Copyright | Copyright 2024 Yoo Chung |
---|---|
License | Apache-2.0 |
Maintainer | dev@chungyc.org |
Safe Haskell | None |
Language | GHC2021 |
Synopsis
- fromExpression :: (Polynomial p e c, Num (p e c), Fractional c) => (Text -> Maybe (p e c), Expression -> Maybe c) -> Expression -> Maybe (p e c)
- forVariable :: (Polynomial p e c, Num (p e c), Fractional c) => Text -> (Text -> Maybe (p e c), Expression -> Maybe c)
- withSymbolicCoefficients :: (Polynomial p e Expression, Num (p e Expression), Integral e) => (Text -> Maybe (p e Expression), Expression -> Maybe Expression) -> (Text -> Maybe (p e Expression), Expression -> Maybe Expression)
- toExpression :: Polynomial p e c => Text -> (c -> Expression) -> p e c -> Expression
- toRationalCoefficient :: Real c => c -> Expression
- toSymbolicCoefficient :: Expression -> Expression
Converting expression to polynomial
fromExpression :: (Polynomial p e c, Num (p e c), Fractional c) => (Text -> Maybe (p e c), Expression -> Maybe c) -> Expression -> Maybe (p e c) Source #
Converts an Expression
into a Polynomial
.
Nothing
will be returned if the conversion is not possible.
Specify the symbol representing the variable for the polynomial with forVariable
.
For example,
>>>
fromExpression (forVariable "x") (("x" + 4) ** 3) :: Maybe IndexedPolynomial
Just x^3 + 12x^2 + 48x + 64
By default, symbols other than the variable for the polynomial are not allowed.
To use symbols representing constants, use withSymbolicCoefficients
as well.
Note that the polynomial type the expression is being converted into
must be able to handle symbolic mathematical expressions for the coefficients.
For example,
>>>
let expr = ("a" + "b") * "x" + "c" :: Expression
>>>
let (Just p) = fromExpression (withSymbolicCoefficients (forVariable "x")) expr :: Maybe IndexedSymbolicPolynomial
>>>
toHaskell $ simplify $ coefficient p 1
"a + b"
The expressions which can be converted must only use negate
, (+)
, (*)
, (-)
,
(/)
with only numbers, coefficients which do not contain the variable,
(**)
with a non-negative integral exponent, and expressions formed thereof.
forVariable :: (Polynomial p e c, Num (p e c), Fractional c) => Text -> (Text -> Maybe (p e c), Expression -> Maybe c) Source #
Specifies the symbol representing the variable for fromExpression
.
withSymbolicCoefficients :: (Polynomial p e Expression, Num (p e Expression), Integral e) => (Text -> Maybe (p e Expression), Expression -> Maybe Expression) -> (Text -> Maybe (p e Expression), Expression -> Maybe Expression) Source #
Specifies that non-variable symbols are allowed for fromExpression
.
The coefficients will be represented by Expression
values.
Converting polynomial to expression
toExpression :: Polynomial p e c => Text -> (c -> Expression) -> p e c -> Expression Source #
Converts a Polynomial
into an Expression
.
The symbol which will represent the variable is the first argument.
How the coefficients are converted must also be specified.
To evaluate the coefficients to an exact rational number,
use toRationalCoefficient
. For example,
>>>
let (Just p) = fromExpression (forVariable "x") (3 * "x"**4 + 1) :: Maybe IndexedPolynomial
>>>
toHaskell $ simplify $ toExpression "x" toRationalCoefficient p
"1 + 3 * x ** 4"
To evaluate the coefficients symbolically, use toSymbolicCoefficient
.
>>>
let (Just p) = fromExpression (withSymbolicCoefficients (forVariable "x")) (("a"+"b") * "x"**4 + 1) :: Maybe IndexedSymbolicPolynomial
>>>
toHaskell $ simplify $ toExpression "x" toSymbolicCoefficient p
"1 + x ** 4 * (a + b)"
toRationalCoefficient :: Real c => c -> Expression Source #
Specifies that coefficients are numbers for toExpression
.
toSymbolicCoefficient :: Expression -> Expression Source #
Specifies that coefficients are symbolic for toExpression
.