Taylor Variables

Purpose

QSS methods rely heavily on root finding and derivative computation. The approximation through Taylor variables transforms any complicated equations to polynomials, which makes root finding cheaper. In addition, the use of Taylor variables provide easily the different derivatives for complex expressions. The implementation includes various constructors and methods to manipulate and evaluate the Taylor series.

In the quantized system solver, and similar to the Taylor struct from the package TaylorSeries.jl, the Taylor0 struct serves as a convenient representation of Taylor series approximations while avoiding memory allocation.

New functions are created to match each old function but with a different name and added caches in the parameters. These new functions are designed to optimize performance by avoiding memory allocation during their execution where arithmetic operations and mathematical functions leverage the existing cached data rather than creating new instances of taylor variables. In addition, the old Taylor is kept, with minimum functionalities, as a fallback in case there is an expression that does not use the available cache vector.

Arithmetic operations: The provided code introduces personalized arithmetic operations such as addsub, subsub, and mulT that are designed to operate directly on existing data structures, utilizing a caching mechanism to store intermediate results.

Mathematical functions: Similar to arithmetic operations, new mathematical functions are designed to leverage in-place operations. Functions like exp, log, sin, and cos, sqrt, power iterate over the input Taylor series and apply the corresponding operation without allocating additional arrays.

Transformation of expressions: The transformF function is designed to translate userdefined mathematical expressions into optimized forms that leverage the custom arithmetic and function implementations. By traversing the expression tree with the prewalk function, it identifies operations such as addition, subtraction, multiplication, division, and specific mathematical functions (like exponential and logarithmic functions). For each identified operation, the code modifies the expression to call specialized versions (e.g., subT, addT, mulT). Additionally, it tracks the number of caches needed for these operations.

Example:

function (+)(a::Taylor0, b::Taylor0)
    v = similar(a.coeffs)
    @__dot__ v = (+)(a.coeffs, b.coeffs)
    return Taylor0(v, a.order)
end

function addT(a::Taylor0, b::Taylor0, cache::Taylor0)
  @__dot__ cache.coeffs = (+)(a.coeffs, b.coeffs)
  return cache
end

The first function (+) from the TaylorSeries.jl creates a new Taylor variable every time, while the customized arithmetic operation addT uses an existing cache.

Taylor references

Taylor0 is defined for many functions. However, other functions can also be added.

QuantizedSystemSolver.Taylor0Type
Taylor0

defines a Taylor Variable. It has the following fields:

  • coeffs: An array of Float64 that holds the coefficients of the Taylor series
  • order: The order of the Taylor series
source

non-allocation operations

QuantizedSystemSolver.createTMethod
createT(a::Taylor0, cache::Taylor0)

puts the coefficients of a into cache and returns cache.

using QuantizedSystemSolver
a = Taylor0([1.0, 1.0])
cache = Taylor0([0.0, 0.0])
createT(a, cache)
cache[0]

# output

1.0
source
QuantizedSystemSolver.addsubMethod
addsub(a::Taylor0, b::Taylor0, c::T, cache::Taylor0) where {T<:Number}

Performs an addition and subtraction operation on Taylor series objects.

Arguments

  • a::Taylor0: The first Taylor series object.
  • b::Taylor0: The second Taylor series object.
  • c::T: A scalar value to be subtracted from the constant term of b.
  • cache::Taylor0: A Taylor series object used as a cache to store the result.

Returns

  • cache::Taylor0: The result of the operation, stored in the cache object.

Description

This function performs the following operations:

  1. Copies the coefficients of b into cache.
  2. Subtracts the scalar c from the constant term of cache.
  3. Adds the coefficients of a to cache.

The result is stored in the cache object and returned.

source
QuantizedSystemSolver.addsubMethod
addsub(a::T, b::Taylor0,c::Taylor0,cache::Taylor0) where {T<:Number}

Example:

Order2 case: cache=[a+b[0]-c[0],b[1]-c[1],b[2]-c[2]]

source
QuantizedSystemSolver.powerTMethod
powerT(a::T, r::S, cache1::Taylor0) where {S<:Real,T<:Number}

Raises a to the power r and stores the result in cache1.

Arguments

  • a::T: The base, a number.
  • r::S: The exponent, a real number.
  • cache1::Taylor0: The cache to store the result.

Returns

  • cache1::Taylor0: The result of a^r.
source
QuantizedSystemSolver.squareMethod
square(a::Taylor0, cache1::Taylor0)

Calculates the square of a and stores the result in cache1.

Arguments

  • a::Taylor0: The input Taylor series.
  • cache1::Taylor0: The cache to store the result.

Returns

  • cache1::Taylor0: The result of a^2.
source

non-allocation functions

Base.sqrtMethod
sqrt(a::Taylor0, cache1::Taylor0)

Calculates the square root of a and stores the result in cache1.

Arguments

  • a::Taylor0: The input Taylor series.
  • cache1::Taylor0: The cache to store the result.

Returns

  • cache1::Taylor0: The result of sqrt(a).
source
Base.sqrtMethod
sqrt(a::T, cache1::Taylor0) where {T<:Number}

Calculates the square root of a and stores the result in cache1.

Arguments

  • a::T: The input number.
  • cache1::Taylor0: The cache to store the result.

Returns

  • cache1::Taylor0: The result of sqrt(a).
source
Base.expMethod
exp(a::Taylor0, c::Taylor0)

Calculates the exponential of a and stores the result in c.

Arguments

  • a::Taylor0: The input Taylor series.
  • c::Taylor0: The cache to store the result.

Returns

  • c::Taylor0: The result of exp(a).
source
Base.expMethod
exp(a::T, c::Taylor0) where {T<:Number}

Calculates the exponential of a and stores the result in c.

Arguments

  • a::T: The input number.
  • c::Taylor0: The cache to store the result.

Returns

  • c::Taylor0: The result of exp(a).
source
Base.logMethod
log(a::Taylor0, c::Taylor0)

Compute the logarithm of a Taylor0 object a and stores the result in Taylor0 object c.

Arguments

  • a::Taylor0: The Taylor0 object for which the logarithm is to be computed.
  • c::Taylor0: to store the result

Returns

  • object c .
source
Base.sinMethod
sin(a::T, s::Taylor0, c::Taylor0) where {T<:Number}

Calculates the sine of the number a and stores the result in the Taylor series s.

Arguments

  • a::T: The input number.
  • s::Taylor0: The Taylor series to store the result.
  • c::Taylor0: An auxiliary Taylor series (not used in this function).

Returns

  • s::Taylor0: The result of sin(a).
source
Base.cosMethod
cos(a::T, s::Taylor0, c::Taylor0) where {T<:Number}

Calculates the cosine of the number a and stores the result in the Taylor series s.

Arguments

  • a::T: The input number.
  • s::Taylor0: The Taylor series to store the result.
  • c::Taylor0: An auxiliary Taylor series (not used in this function).

Returns

  • s::Taylor0: The result of cos(a).
source
Base.tanMethod
tan(a::Taylor0, c::Taylor0, c2::Taylor0)

Calculates the tangent of the Taylor series a and stores the result in c.

Arguments

  • a::Taylor0: The input Taylor series.
  • c::Taylor0: The Taylor series to store the result.
  • c2::Taylor0: An auxiliary Taylor series used in the calculation.

Returns

  • c::Taylor0: The result of tan(a).
source
Base.asinMethod
asin(a::Taylor0, c::Taylor0, r::Taylor0, cache3::Taylor0)

Calculates the arcsine of a and stores the result in c.

Arguments

  • a::Taylor0: The input Taylor series.
  • c::Taylor0: The cache to store the result.
  • r::Taylor0: An auxiliary Taylor series.
  • cache3::Taylor0: Another auxiliary Taylor series.

Returns

  • c::Taylor0: The result of acos(a).
source
Base.acosMethod
acos(a::Taylor0, c::Taylor0, r::Taylor0, cache3::Taylor0)

Calculates the arccosine of a and stores the result in c.

Arguments

  • a::Taylor0: The input Taylor series.
  • c::Taylor0: The cache to store the result.
  • r::Taylor0: An auxiliary Taylor series.
  • cache3::Taylor0: Another auxiliary Taylor series.

Returns

  • c::Taylor0: The result of acos(a).
source
Base.atanMethod
atan(a::Taylor0, c::Taylor0, r::Taylor0)

Calculates the arctangent of a and stores the result in c.

Arguments

  • a::Taylor0: The input Taylor series.
  • c::Taylor0: The cache to store the result.
  • r::Taylor0: An auxiliary Taylor series.

Returns

  • c::Taylor0: The result of atan(a).
source
Base.absMethod
abs(a::Taylor0, cache1::Taylor0)

Compute the absolute value of a Taylor0 object a and store the result in cache1.

Arguments

  • a::Taylor0: The input Taylor0 object whose absolute value is to be computed.
  • cache1::Taylor0: A Taylor0 object to store the result of the absolute value computation.

Returns

  • cache1::Taylor0: The Taylor0 object containing the absolute value of a.
source

Internals

All internals functions of the TaylorSeries.jl are used with the exception of the following two functions, which are rewritten to avoid one allocation.

QuantizedSystemSolver.asin!Function
asin!(c::Taylor0, a::Taylor0, r::Taylor0, cache3::Taylor0, k::Int)

Compute the arcsine of a Taylor series a and store the result in c. The computation uses intermediate results stored in r and cache3. The parameter k specifies the order of the Taylor series expansion. This is a copy of the function asin! as written in the package TaylorSeries.jl but with an added cache to avoid allocation.

Arguments

  • c::Taylor0: The Taylor series where the result will be stored.
  • a::Taylor0: The input Taylor series for which the arcsine is computed.
  • r::Taylor0: An intermediate Taylor series used in the computation.
  • cache3::Taylor0: Another intermediate Taylor series used in the computation to avoid one allocation of function square inside.
  • k::Int: The order of the Taylor series expansion.

Returns

  • nothing: The result of the arcsine computation stored in c.
source
QuantizedSystemSolver.acos!Function
    acos!(c::Taylor0, a::Taylor0, r::Taylor0, cache3::Taylor0, k::Int)

Compute the arccosine of a Taylor series a and store the result in c. The computation uses intermediate results stored in r and cache3. The parameter k specifies the order of the Taylor series expansion. This is a copy of the function acos! as written in the package TaylorSeries.jl but with an added cache to avoid one allocation.

Arguments

  • c::Taylor0: The Taylor series where the result will be stored.
  • a::Taylor0: The input Taylor series for which the arcsine is computed.
  • r::Taylor0: An intermediate Taylor series used in the computation.
  • cache3::Taylor0: Another intermediate Taylor series used in the computation to avoid allocation of function square inside.
  • k::Int: The order of the Taylor series expansion.

Returns

  • nothing: The result of the arcsine computation stored in c.
source
Allocating TaylorSeries functions

As a fallback all TaylorSeries functions are used in case an expression is not transformed to one of the existing personalized functions above.

Index