uiua-math
FFTConvolve
FFTConvolve module
Rank-polymorphic convolution using fast fourier transform
FFTConvolve~Full function
Full convolution wherever any overlap between the inputs exists
Source code
Full ← ⍥R=0↥⊃∩type(×√/×⊸△⍜∩fft×∩⌞⬚0↙-1+◡∩△)FFTConvolve~Valid function
Convolution of only instances of complete overlap between inputs
Source code
Valid ← ⍥R=0↥⊃∩type(↘-+1⌵⊙⟜(×√/×⊸△⍜∩fft×∩⌞⬚0↙)⊃-↥◡∩△)FFTConvolve~Same function
Convolution that maintains the largest of the input dimensions
Source code
Same ← ↙⟜(↻⌊÷₂-)⊙⊸△↥⊃∩△FullFFTConvolve~Wrap function
Wrapping convolution producing the same shape as the largest input
Source code
Wrap ← ⍥R=0↥⊃∩type(×√/×⊸△⍜∩fft×∩⌞⬚0↙↥◡∩△)Data types
OdeConfig data boxed
Configuration parameters for the Ode‼ macro
Index macros
Ode‼! index macro
More configurable version of Ode‼
The first of the three functions can be used to set optional arguments:
ε- error boundHMin- minimum step sizeHMax- maximum step sizeH- initial step size The remaining two functions correspond to those ofOde‼.
Example: Changing simulation precision. This sets the simulation precision threshold to a larger value (meaning less precision, with a cheaper simulation).
g ← 9.81 # GravityL ← 1 # Pendulum lengthμ ← 0.5 # Damping coefficient
[0 12] # Initial position of 0 and velocity of 120 # Initial time of 0Ode‼!(°⊸ε 1e¯3|⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]|<10)
Source code
Ode‼! ← OdeImpl‼^1^2 OdeConfig!(^0($"_New"˜▽@⊙-1⊢⋅⊢)^!^0)Ode‼ index macro
Numerical integrator
Takes two functions and two values.
- The first function should take a time value and a state vector and output the derivative of the state vector.
- The second function should take anywhere from 1 to 4 arguments and return 0 or 1 for whether to continue simulating. Based on how many inputs the second function takes, it will be passed:
- The current time
- The current state vector
- The array of all past times
- The array of all past state vectors
The two values should respectively be the initial time and the initial state vector.
Example: Simulating a damped pendulum. This simulates the motion of a damped pendulum that begins vertical with an initial angular velocity of 12 radians per second.
g ← 9.81 # GravityL ← 1 # Pendulum lengthμ ← 0.5 # Damping coefficient
[0 12] # Initial position of 0 and velocity of 120 # Initial time of 0Ode‼(⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]|<10)
Uses Runge-Kutta-Fehlberg 4th-5th-order variable-step-size numerical integration.
Source code
Ode‼ ← Ode‼!∘^0^1OdeT‼ index macro
More configurable version of OdeT!
The first of the two functions can be used to set optional arguments:
ε- error boundHMin- minimum step sizeHMax- maximum step sizeH- initial step size The remaining function corresponds to that ofOdeT!.
Example: Changing simulation precision. This sets the simulation precision threshold to a larger value (meaning less precision, with a cheaper simulation).
g ← 9.81 # GravityL ← 1 # Pendulum lengthμ ← 0.5 # Damping coefficient
[0 12] # Initial position of 0 and velocity of 12⍜×⇡5 10 # Time from 0 to 10 at 5 samples per secondOdeT‼(°⊸ε 1e¯3|⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟])
Source code
OdeT‼ ← ( OdeConfig!⊃⊓C‼^0◌∘( ⤙⊓C‼^0∘◌⊓C‼^0∘⊃(/↥|/↧⧈-⍆|/↧) ⬚∘Ode‼!(^0⊓C‼^0∘°⊸HMax|^1|<°◌) ) ⟜(⤚⊏⊙⧈⊟-₁˜⨂1⊸>⌟) ˜÷˜-⊙≡⊃⊢/- ⊙(⍜⊙-×|°⊟⤸₁⊏|⧈⊟))OdeT! index macro
Numerical integrator
Takes a function and two values. The function should take a time value and a state vector and output the derivative of the state vector. The two values should be as follows:
- A list of all times to output samples at
- The initial state vector, interpreted to be the state at time equal to the smallest value in the time value list
Example: Simulating a damped pendulum. This simulates the motion of a damped pendulum that begins vertical with an initial angular velocity of 12 radians per second.
g ← 9.81 # GravityL ← 1 # Pendulum lengthμ ← 0.5 # Damping coefficient
[0 12] # Initial position of 0 and velocity of 12⍜×⇡5 10 # Time from 0 to 10 at 5 samples per secondOdeT!⋅⊃[⊣|¯+˜∩×g μ÷L∿°⊟]
Uses Runge-Kutta-Fehlberg 4th-5th-order variable-step-size numerical integration.
Source code
OdeT! ← OdeT‼∘^Monadic functions
Mean function
Arithmetic mean
Source code
Mean ← ÷⧻⟜/+GMean function
Geometric mean
Source code
GMean ← ⁿ˜÷1⧻⟜/×HMean function
Harmonic mean
Source code
HMean ← ÷/+⟜⧻˜÷1QMean function
Quadratic mean
Source code
QMean ← √÷⧻⟜/+˙×Median function
Median TODO: O(n) median algorithm?
Source code
Median ← ÷2/+⊏⊟⊃⌊⌈÷2-1⊸⧻⍆Var function
Variance
Source code
Var ← -˙×∩÷∩⊃⧻/+⟜˙×Stdev function
Standard deviation
Source code
Stdev ← √VarSin function
Sine
Source code
Sin ← ∿Cos function
Cosine
Source code
Cos ← ∿˜-ηTan function
Tangent
Source code
Tan ← ⌅(÷°∠|∠⊙1)Csc function
Cosecant
Source code
Csc ← ⨪SinSec function
Secant
Source code
Sec ← ⨪CosCot function
Cotangent
Source code
Cot ← Tan˜-ηSinh function
Hyperbolic Sine
Source code
Sinh ← ⌅(÷₂-∩ₑ⊸¯|°ₑ+⟜⍜˙×+₁)Cosh function
Hyperbolic Cosine
Source code
Cosh ← ⌅(÷₂+∩ₑ⊸¯|°ₑ+⟜⍜˙×-₁)Tanh function
Hyperbolic Tangent
Source code
Tanh ← ⌅(÷⊃+-1ₑ×₂|÷₂°ₑ÷˜⊃-+1)Csch function
Hyperbolic Cosecant
Source code
Csch ← ⨪SinhSech function
Hyperbolic Secant
Source code
Sech ← ⨪CoshCoth function
Hyperbolic Cotangent
Source code
Coth ← ⨪TanhMDet function
Matrix Determinant
Source code
MDet ← ◌⍣GaussEliminate‼(⍥¯∪∪/≠)∪∪× ⊙0⊙1Mgje function
Perform Gauss-Jordan elimination
Source code
Mgje ← ⊏⍏˜⨂1>1e¯12⊸⌵ ⍥( ↻₁⍣(⍜⊙⊢÷⟜(-⊞×⊙⊸⊢⊂0÷)°⊂⊸≡⊏⊢)◌⊚>1e¯12⌵⊸⊢)⊸⧻MInv function
Matrix Inverse
Source code
MInv ← |1 ⌅(⍜⍉(↘÷2⊸⧻) ⍣Mgje(⍤"Matrix has no inverse"0) ˜≡⊂˙⊞=°⊏|MInv)Fact function
Pervasive factorial
Source code
Fact ← /×°⍉+1⬚0≡₀⇡Gamma function
Gamma function Γ(z)
Source code
Gamma ← ( -1/2 ⟜( -1 # From https://en.wikipedia.org/wiki/Lanczos_approximation °⊏[ 0.9999999999999999 1975.3739023578853 ¯4397.382392792243 3462.6328459862716 ¯1156.9851431631168 154.53815050252774 ¯6.253671612368916 0.034642762454736804 ¯7.477617197444297e¯7 6.304125382185226e¯8 ¯2.7405717035683877e¯8 4.048694881756761e¯9 ] /+÷⍜⊢⋅1≡+⊙˜¤ ) ××× √τ ₑ¯ ⤙ⁿ ⤙+ 8 # g)Divisors function
Divisors of N (including 1 and N) The values are in increasing order and distinct.
Source code
Divisors ← ◌°▽⊂⊃÷⇌ ▽=0⊸⊃◿÷ +1⇡⌊⊸√Erf function
Error function
Source code
Erf ← ( # From https://www.johndcook.com/blog/cpp_erf ˜÷1+1×0.3275911 ⟜(˜ⁿe¯˙×) ⌵⟜± ⊙[1.061405429 ¯1.453152027 1.421413741 ¯0.284496736 0.254829592 ]⊙0 ׬×∧(×⊙+)¤)PSet function
Powerset of a list
Source code
PSet ← ⍚▽⋯⇡˜ⁿ2⧻⟜¤Dyadic functions
MCof function
Matrix Cofactors
Source code
MCof ← ⍥¯˙⊞≠◿2°⊏MSquarify≡(MDet MSquarify≡⊡⊙¤⊚¬⊞↥°⊟)⊙¤⬚0≡₀°⊚⊚⊸⊸=Dot function
Dot product
Source code
Dot ← /+×Cross function
Cross product
Source code
Cross ← ↻2-∩(×↻2)◡FMmp function
Matrix product
Computes AB
Source code
Mmp ← |2 ⌅(⊞Dot⊙⍉|◌⍜≡⊂Mgje)Mvp function
Matrix-vector product
Computes MV where V is represented as a 1D list of numbers
Source code
Mvp ← |2 ⌅(Dot⍉|♭◌⍜≡⊂Mgje)Mpow function
Matrix power
Source code
Mpow ← ◌⍥⟜Mmp⊙(F˙⊞=°⊏)Qqp function
Quaternion product
Computes PQ for quaternion arrays P and Q.
Semi-pervasive; the last axis of the inputs must be of length 4, and will be interpreted as the input quaternions’ real, i, j, and k.
# Computes (1+2i+3j+4k)(1+3i+5j+7k) and (2+4i+6j+8k)(5+6i+7j+8k)Qqp [1_2_3_4 2_4_6_8] [1_3_5_7 5_6_7_8]## ╭─## ╷ ¯48 6 6 12## ¯120 24 60 48## ╯
Source code
Qqp ← ⍉⊕/+-1⌵⊙×⟜±QqpMask⊞×∩°⍉RQuat function
Create 3D rotation quaternions.
Expects an angle array θ and a unit vector axis array U.
Semi-pervasive; the unit vector array must have one extra trailing axis compared to the angle array, and that axis must be length 3.
For each angle-vector pair, computes cos(θ/2) + Usin(θ/2).
# Creates two rotation quaternions:# - η radians about 0_0_1# - π radians about 3/5_4/5_0RQuat η_π [0_0_1 3/5_4/5_0]
To use rotation quaternions created by RQuat to rotate 3D
vectors, see QRot.
Source code
RQuat ← ⍜⊙°⍉⊂˜⊙×°∠÷2QRot function
Rotate a 3D vector array using a quaternion array.
To create rotation quaternions to use with QRot, see RQuat.
Expects a quaternion array Q and a 3D vector array V.
Semi-pervasive; the input arrays should have matching shapes aside from the final axis, which should be of lengths 4 and 3 respectively.
For each quaternion-vector pair, computes Q * V * Q’.
RQuat η 0_0_1 # Create a rotation by η radians about 0_0_1QRot ¤⊙[3_2_0 1_3_2] # Use the quaternion to rotate 3_2_0 and 1_3_2
QRot can be used with ⍜ to undo the rotations afterward.
Source code
QRot ← ⌅(QRotImpl|QRotImpl⍜(↘1°⍉)¯)GCD function
Greatest common divisor
Ignores sign
Source code
GCD ← ◌⍢⤚◿±⌵LCM function
Least common multiple
Ignores sign
Source code
LCM ← ⌵÷⊃GCD×Base function
Encode an array of numbers into digits of a given base
Analogous to ⋯ for base 2; the least significant digit is first.
Base allows multiple bases as input.
# Encodes 6 in base 2, 7 in base 3, 8 in base 4, and 9 in base 5.Base [2_3 4_5] [6_7 8_9]## ╭─## ╷ 0 1 1## ╷ 1 2 0#### 0 2 0## 4 1 0## ╯
You can use ⌝Base to decode from a base/bases.
# Decodes 6 in base 2, 7 in base 3, 8 in base 4, and 9 in base 5.⌝Base [2_3 4_5] [[0_1_1 1_2_0] [0_2_0 4_1_0]]## ╭─## ╷ 6 7## 8 9## ╯
Source code
Base ← ⌅( ⍉◌∧⊃◿(⊂¤⌊÷)ⁿ⊙¤⇌⇡/↥♭+1⌊⊃⌝˜ⁿ⊙⊙[]| /+×ⁿ⊙¤⇡◡⋅⧻⊙°⍉)ModInv function
Inverse of N modulo M (M if nonexistent)
Source code
ModInv ← ˜⨂1˜◿×◡⋅⇡ModOrd function
Multiplicative order of N modulo M
Source code
ModOrd ← ⊡1⊚=1˜◿ⁿ◡⋅⇡CFrac function
Convert a number X to a continued fraction to N terms.
Use °CFrac to convert to a number from a continued fraction.
Source code
CFrac ← ⌅(⍥⊃(÷⤚◿1)⌊|⊃⧻/(+˜÷1)⇌)BoxMuller function
Box-Muller transform
Converts uniformly distributed pairs of input values to standard normally distributed pairs of output values
Source code
BoxMuller ← ∩×⤙⊓°∠∘×τF√ׯ2°ₑ¬Gaussian function
Generate a 2 dimensional square Gaussian kernel
Source code
Gaussian ← ÷/+⊸♭ₑ¯÷2˙ט÷⌵˙⊞ℂ-÷2-1⟜⇡GeomPmf function
Geometric distribution
Probability of X failures before the first success of repeated trials with success probability p
Source code
GeomPmf ← ×⟜(˜ⁿ¬)GeomCmf function
Cumulative geometric distribution
Probability of X or fewer failures before the first success of repeated trials with success probability p
Source code
GeomCmf ← ⍜¬˜ⁿ⊙(+1)PoissonPmf function
Poisson distribution
Probability of X occurrences of an unlikely event over many trials with expected value λ
Source code
PoissonPmf ← ÷⊙×⊃(⋅Fact|˜ⁿ|ₑ¯)PoissonCmf function
Cumulative Poisson distribution
Probability of X or fewer occurrences of an unlikely event over many trials with expected value λ
Source code
PoissonCmf ← /+PoissonPmf⊓¤(⇡+1)Triadic functions
DistRand function
Seeded random indices from a distribution array
Given a seed, a shape, and a probability distribution array
of any rank containing positive numbers, DistRand normalizes
the distribution if necessary so it sums to 1, then returns
an array of deep indices into the distribution arrays, chosen
randomly weighted by the distribution. The generated random
indices will fill the shape given to the function. If the
distribution array is rank 1, the output will have the shape
given. If the distribution array is rank >1, the output will
have a shape equal to the shape given suffixed with the rank of
the distribution array.
# Generate random indices in the shape 2_3 with a seed of 8DistRand [0.1 0.4 0.3 0.2] 2_3 8## ╭─## ╷ 1 2 3## 2 1 2## ╯
Source code
DistRand ← ˜⍜♭≡(⊢⊚>)⊓¤gen⍜♭\+÷/+⊸♭BinomPmf function
Binomial distribution
Probability that n Bernoulli trials each with success probability p will amount to X successes
Source code
BinomPmf ← ××⊃(≡₀⧅>|˜⋅ⁿ|ⁿ⊓-¬) ⤚⋅FBinomCmf function
Cumulative binomial distribution
Probability that n Bernoulli trials each with success probability p will amount to X or fewer successes
NOTE: This function only accepts scalars for X. Use each or rows if multiple X values are desired
Source code
BinomCmf ← /+BinomPmf⊓∩¤(⇡+1)NormalPdf function
Normal distribution
Probability density of a gaussian/bell curve with mean μ and standard deviation σ
Source code
NormalPdf ← ÷⊃(×√τ|ₑ÷2¯˙×÷⊙-)NormalCdf function
Cumulative normal distribution
Probability that a normally distributed random variable with mean μ and standard deviation σ is less than X
Source code
NormalCdf ← ÷2+1Erf÷×√2⊙-Quad function
Po-Shen quadratic solver
Returns roots as a list
Outputs complex-typed numbers only if the roots are complex
Source code
Quad ← ⍥R⌵˜↥₀⊟⊃-+⊙F√⟜±˜-˙×⟜Fℂ₀¯÷₂∩⌞÷