pyoptmat.utility: useful miscellaneous mathematical functions
Various utility functions used in the rest of the modules. This includes basic mathematical functions, routines used in the tests, and various visualization routines.
- class pyoptmat.utility.ArbitraryBatchTimeSeriesInterpolator(times, data)
Bases:
ModuleInterpolate
datalocated at discretetimeslinearly to pointt.This version handles batched of arbitrary size – only the rightmost batch dimension must agree with the input data. All other dimensions are broadcast.
- Parameters:
times (torch.tensor) – input time series as a code:(ntime,nbatch) array
values (torch.tensor) – input values series as a code:(ntime,nbatch) array
- forward(t)
Calculate the linearly-interpolated current values
- Parameters:
t (torch.tensor) – batched times as
(...,nbatch,)array- Returns:
batched values at
t- Return type:
torch.tensor
- class pyoptmat.utility.BatchTimeSeriesInterpolator(times, data)
Bases:
ModuleInterpolate
datalocated at discretetimeslinearly to pointt.This version handles batched input
Precache a lot of the work required to interpolate in time vs
pyoptmat.utility.timeseries_interpolate_batch_times()- Parameters:
times (torch.tensor) – input time series as a code:(ntime,nbatch) array
values (torch.tensor) – input values series as a code:(ntime,nbatch) array
- forward(t)
Calculate the linearly-interpolated current values
- Parameters:
t (torch.tensor) – batched times as
(nbatch,)array- Returns:
batched values at
t- Return type:
torch.tensor
- class pyoptmat.utility.CheaterBatchTimeSeriesInterpolator(times, data)
Bases:
ModuleInterpolate
datalocated at discretetimeslinearly to pointt.Precache a lot of the work required to interpolate in time vs
pyoptmat.utility.timeseries_interpolate_batch_times()This is the cheater version specifically for our structured problems where if you figure out where one time point index is relative to the provided time points then you can use that index for all the other points in the batch. This won’t work in general, but works fine here.
- Parameters:
times (torch.tensor) – input time series as a
(ntime,nbatch)arrayvalues (torch.tensor) – input values series as a
(ntime,nbatch)array
- forward(t)
Calculate the linearly-interpolated current values
- Parameters:
t (torch.tensor) – batched times as
(nbatch,)array- Returns:
batched values at
t- Return type:
torch.tensor
- pyoptmat.utility.add_id(df)
Add the identity to a tensor with the shape of the Jacobian
- Parameters:
df (torch.tensor) – batched (n,m,m) tensor
- Returns:
dfplus a batched identity of the right shape- Return type:
torch.tensor
- pyoptmat.utility.batch_differentiate(fn, x0, eps=1e-06, nbatch_dim=1)
New numerical differentiation function to handle the batched-model cases
This version handles arbitrary batch sizes
- Parameters:
fn (torch.tensor) – function to differentiate via finite differences
x0 (torch.tensor) – point at which to take the numerical derivative
- Keyword Arguments:
eps (float) – perturbation to use
- Returns:
- finite difference approximation to
\(\frac{df}{dx}|_{x_0}\)
- Return type:
torch.tensor
- pyoptmat.utility.compose(f1, f2)
A function composition operator…
- Parameters:
f1 (function) – first function
f2 (function) – second function
- Returns:
f2(f1)
- Return type:
function
- pyoptmat.utility.differentiate(fn, x0, eps=1e-06)
Numerical differentiation used in the tests, old version does not handle batched input
- Parameters:
fn (torch.tensor) – function to differentiate via finite differences
x0 (torch.tensor) – point at which to take the numerical derivative
- Keyword Arguments:
eps (float) – perturbation to use
- Returns:
- finite difference approximation to
\(\frac{df}{dx}|_{x_0}\)
- Return type:
torch.tensor
- pyoptmat.utility.heaviside(X)
A pytorch-differentiable version of the Heaviside function
\[H\left(x\right) = \frac{\operatorname{sign}(x) + 1)}{2}\]- Parameters:
X (torch.tensor) – tensor input
- Returns:
the Heaviside function of the input
- Return type:
torch.tensor
- pyoptmat.utility.jacobianize(argnums=None)
Decorator that adds the multibatched Jacobian to a function
Assumes that the function itself has only a single dimension, the last in the shape
By default provides Jacobian for all *args, but argnums can be set to limit this to certain arguments
- pyoptmat.utility.macaulay(X)
A pytorch-differentiable version of the Macualay bracket
\[M\left(x\right) = x H\left(x\right)\]- Parameters:
X (torch.tensor) – tensor input
- Returns:
the Macaulay bracket applied to the input
- Return type:
torch.tensor
- pyoptmat.utility.mbmm(A1, A2)
Batched matrix-matrix multiplication with several batch dimensions
- pyoptmat.utility.new_differentiate(fn, x0, eps=1e-06)
New numerical differentiation function to handle the batched-model cases
- Parameters:
fn (torch.tensor) – function to differentiate via finite differences
x0 (torch.tensor) – point at which to take the numerical derivative
- Keyword Arguments:
eps (float) – perturbation to use
- Returns:
- finite difference approximation to
\(\frac{df}{dx}|_{x_0}\)
- Return type:
torch.tensor
- pyoptmat.utility.timeseries_interpolate_batch_times(times, values, t)
Interpolate the time series defined by X to the times defined by t
This version handles batched input
- Parameters:
times (torch.tensor) – input time series as a
(ntime,nbatch)arrayvalues (torch.tensor) – input value series as a
(ntime,nbatch)arrayt (torch.tensor) – batch times as a
(nbatch,)array
- Returns:
Interpolated values as a
(nbatch,)array- Return type:
torch.tensor
- pyoptmat.utility.timeseries_interpolate_single_times(times, values, t)
Interpolate the time series defined by X to the times defined by t
This version does not handle batched input
- Parameters:
times (torch.tensor) – input time series as a
(ntime,)arrayvalues (torch.tensor) – input value series as a
(ntime,nbatch)arrayt (torch.tensor) – times as a scalar
- Returns:
interpolated values as a
(nbatch,)array- Return type:
torch.tensor
- pyoptmat.utility.visualize_variance(strain, stress_true, stress_calc, alpha=0.05)
Visualize variance for batched examples
- Parameters:
strain (torch.tensor) – input strain
stress_true (torch.tensor) – actual stress values
stress_calc (torch.tensor) – simulated stress values
- Keyword Arguments:
alpha (float) – alpha value for shading