-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
function _generateBasis!(eqs, f, x, coeffs) | ||
n_x = size(x, 1) | ||
@assert length(eqs) == size(x, 1)*length(coeffs) | ||
@inbounds for (i, ti) in enumerate(coeffs) | ||
eqs[(i-1)*n_x+1:i*n_x] .= f(x, ti) | ||
end | ||
return | ||
end | ||
|
||
|
||
""" | ||
chebyshev_basis(x, c) | ||
Constructs an array containing a Chebyshev basis in the variables `x` with coefficients `c`. | ||
If `c` is an `Int` returns all coefficients from 1 to `c`. | ||
""" | ||
function chebyshev_basis(x::Array{Operation}, coefficients::AbstractVector) | ||
eqs = Array{Operation}(undef, size(x, 1)*length(coefficients)) | ||
f(x, t) = cos.(t .* acos.(x)) | ||
_generateBasis!(eqs, f, x, coefficients) | ||
eqs | ||
end | ||
|
||
chebyshev_basis(x::Array{Operation}, terms::Int) = chebyshev_basis(x, 1:terms) | ||
|
||
|
||
""" | ||
sin_basis(x, c) | ||
Constructs an array containing a Sine basis in the variables `x` with coefficients `c`. | ||
If `c` is an `Int` returns all coefficients from 1 to `c`. | ||
""" | ||
function sin_basis(x::Array{Operation}, coefficients::AbstractVector) | ||
eqs = Array{Operation}(undef, size(x, 1)*length(coefficients)) | ||
f(x, t) = sin.(t .* x) | ||
_generateBasis!(eqs, f, x, coefficients) | ||
eqs | ||
end | ||
|
||
sin_basis(x::Array{Operation}, terms::Int) = sin_basis(x, 1:terms) | ||
|
||
|
||
""" | ||
cos_basis(x, c) | ||
Constructs an array containing a Cosine basis in the variables `x` with coefficients `c`. | ||
If `c` is an `Int` returns all coefficients from 1 to `c`. | ||
""" | ||
function cos_basis(x::Array{Operation}, coefficients::AbstractVector) | ||
eqs = Array{Operation}(undef, size(x, 1)*length(coefficients)) | ||
f(x, t) = cos.(t .* x) | ||
_generateBasis!(eqs, f, x, coefficients) | ||
eqs | ||
end | ||
|
||
cos_basis(x::Array{Operation}, terms::Int) = cos_basis(x, 1:terms) | ||
|
||
|
||
""" | ||
fourier_basis(x, c) | ||
Constructs an array containing a Fourier basis in the variables `x` with (integer) coefficients `c`. | ||
If `c` is an `Int` returns all coefficients from 1 to `c`. | ||
""" | ||
function fourier_basis(x::Array{Operation}, coefficients::AbstractVector{Int}) | ||
eqs = Array{Operation}(undef, size(x, 1)*length(coefficients)) | ||
f(x, t) = iseven(t) ? cos.(t .* x ./ 2) : sin.(t .* x ./2) | ||
_generateBasis!(eqs, f, x, coefficients) | ||
eqs | ||
end | ||
|
||
fourier_basis(x::Array{Operation}, terms::Int) = fourier_basis(x, 1:terms) | ||
|
||
""" | ||
polynomial_basis(x, c) | ||
Constructs an array containing a polynomial basis in the variables `x` up to degree `c` of the form | ||
`[x₁, x₂, x₃, ..., x₁^1 * x₂^(c-1)]`. Mixed terms are included. | ||
""" | ||
function polynomial_basis(x::Array{Operation}, degree::Int = 1) | ||
@assert degree > 0 | ||
n_x = length(x) | ||
n_c = binomial(n_x+degree, degree) | ||
eqs = Array{Operation}(undef, n_c) | ||
_check_degree(x) = sum(x)<=degree ? true : false | ||
itr = Base.Iterators.product([0:degree for i in 1:n_x]...) | ||
itr_ = Base.Iterators.Stateful(Base.Iterators.filter(_check_degree, itr)) | ||
filled = false | ||
@inbounds for i in 1:n_c | ||
eqs[i] = ModelingToolkit.Constant(1) | ||
filled = true | ||
for (xi, ci) in zip(x, popfirst!(itr_)) | ||
if !iszero(ci) | ||
filled ? eqs[i] = xi^ci : eqs[i] *= xi^ci | ||
filled = false | ||
end | ||
end | ||
end | ||
eqs | ||
end | ||
|
||
|
||
""" | ||
monomial_basis(x, c) | ||
Constructs an array containing monomial basis in the variables `x` up to degree `c` of the form | ||
`[x₁, x₁^2, ... , x₁^c, x₂, x₂^2, ...]`. | ||
""" | ||
function monomial_basis(x::AbstractArray{Operation}, degree::Int = 1) | ||
@assert degree > 0 | ||
n_x = length(x) | ||
exponents = 1:degree | ||
n_e = length(exponents) | ||
n_c = n_x * n_e | ||
eqs = Array{Operation}(undef, n_c) | ||
idx = 0 | ||
for i in 1:n_x, j in 1:n_e | ||
idx = (i-1)*n_e+j | ||
eqs[idx] = x[i]^exponents[j] | ||
end | ||
eqs | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters