math#

gamspy.math.cos(x: int | float | Symbol) Expression[source]#

Cosine of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import cos
>>> import numpy as np
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = cos(np.pi)
>>> r.toValue()
np.float64(-1.0)
gamspy.math.cosh(x: int | float | Symbol) Expression[source]#

Hyperbolic cosine of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import cosh
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = cosh(0)
>>> r.toValue()
np.float64(1.0)
gamspy.math.sin(x: float | Symbol) Expression[source]#

Sine of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import sin
>>> import numpy as np
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = sin(np.pi/2)
>>> r.toValue()
np.float64(1.0)
gamspy.math.sinh(x: float | Symbol) Expression[source]#

Hyperbolic sine of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import sinh
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = sinh(0)
>>> r.toValue()
np.float64(0.0)
gamspy.math.acos(x: float | Symbol) Expression[source]#

Inverse cosine of x.

Returns:
Expresion | float

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import acos
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = acos(1)
>>> r.toValue()
np.float64(0.0)
gamspy.math.asin(x: float | Symbol) Expression[source]#

Inver sinus of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import asin
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = asin(0)
>>> r.toValue()
np.float64(0.0)
gamspy.math.tan(x: float | Symbol) Expression[source]#

Tangent of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import tan
>>> import numpy as np
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = tan(np.pi/4)
>>> round(r.toValue(), 2)
np.float64(1.0)
gamspy.math.tanh(x: float | Symbol) Expression[source]#

Hyperbolic tangent of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import tanh
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = tanh(0)
>>> r.toValue()
np.float64(0.0)
gamspy.math.atan(x: float | Symbol) Expression[source]#

Inverse tangent of x.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import atan
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = atan(0)
>>> r.toValue()
np.float64(0.0)
gamspy.math.atan2(y: int | float | Symbol, x: int | float | Symbol) Expression[source]#

Four-quadrant arctan function yielding arctan(y/x), which is the angle the vector (x,y) makes with (1,0) in radians.

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import atan2
>>> m = Container()
>>> r = Parameter(m, "r")
>>> r[...] = atan2(1,1)
>>> r.toValue()
np.float64(0.7853981633974483)
gamspy.math.exp(x: float | Symbol) Expression[source]#

Exponential of x (i.e. e^x)

Parameters:
xfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import exp
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = exp(a)
>>> b.toValue()
np.float64(44.701184493300815)
gamspy.math.power(base: float | Operable, exponent: float | Symbol) Expression[source]#

Base to the exponent power (i.e. base ^ exponent)

Parameters:
basefloat | Symbol
exponentfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import power
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = power(a, 3)
>>> b.toValue()
np.float64(54.87199999999999)
gamspy.math.sqr(x: float | Symbol) Expression[source]#

Square of x (i.e. x^2)

Parameters:
xfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import sqr
>>> m = Container()
>>> a = Parameter(m, "a", records=4)
>>> b = Parameter(m, "b")
>>> b[...] = sqr(a)
>>> b.toValue()
np.float64(16.0)
gamspy.math.sqrt(x: int | float | Symbol, safe_cancel: bool = False) Expression[source]#

Square root of x

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import sqrt
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 54), ("i3", 0)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = sqrt(a[i])
>>> b.toList()
[('i1', 2.0), ('i2', 7.3484692283495345)]
gamspy.math.log(x: int | float | Symbol) Expression[source]#

Natural logarithm of x (i.e. logarithm base e of x)

Parameters:
xint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import log
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = log(a)
>>> b.toValue()
np.float64(1.33500106673234)
gamspy.math.log2(x: float | Symbol) Expression[source]#

Binary logarithm (i.e. logarithm base 2 of x)

Parameters:
xfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import log2
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = log2(a)
>>> b.toValue()
np.float64(1.9259994185562224)
gamspy.math.log10(x: float | Symbol) Expression[source]#

Common logarithm (i.e. logarithm base 10 of x)

Parameters:
xfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import log10
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = log10(a)
>>> b.toValue()
np.float64(0.5797835966168101)
gamspy.math.log_beta(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Log beta function (i.e. log(B(x, y))

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> import math
>>> from gamspy import Container, Parameter
>>> from gamspy.math import log_beta
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = log_beta(a,5)
>>> math.isclose(b.toValue(), -5.45446741772822)
True
gamspy.math.log_gamma(x: int | float | Symbol) Expression[source]#

Log gamma function of x

Parameters:
xint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import log_gamma
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = log_gamma(a)
gamspy.math.logit(x: int | float | Symbol) Expression[source]#

Logit Transformation (i.e. log(x / (1 - x))) for x in (0, 1)

Parameters:
xint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import logit
>>> m = Container()
>>> a = Parameter(m, "a", records=0.8)
>>> b = Parameter(m, "b")
>>> b[...] = logit(a)
>>> b.toValue()
np.float64(1.3862943611198908)
gamspy.math.permute(x: Parameter | ImplicitParameter | Variable | ImplicitVariable, dims: list[int]) ImplicitVariable | ImplicitParameter[source]#

Permutes the dimensions provided input x using dim. Similar to PyTorch permute.

Parameters:
x: (

Parameter | implicits.ImplicitParameter | Variable | implicits.ImplicitVariable

)
dims: list[int]
Returns:
implicits.ImplicitVariable | implicits.ImplicitParameter

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, name="i")
>>> j = gp.Set(m, name="j")
>>> k = gp.Set(m, name="k")
>>> p = gp.Parameter(m, name="p", domain=[i, j, k])
>>> p2 = gp.math.permute(p, [2, 0, 1])
>>> p2.domain
[Set(name=k, domain=['*']), Set(name=i, domain=['*']), Set(name=j, domain=['*'])]
gamspy.math.trace(x: Parameter | implicits.ImplicitParameter | Variable | implicits.ImplicitVariable, axis1: int = 0, axis2: int = 1) Operation[source]#

Returns trace of the given input x. By default trace of zeroth and first axis used. axis1 and axis2 parameters control on which axes to get trace. Domains at the axis1 and axis2 must be same or aliases.

Parameters:
x: (

Parameter | implicits.ImplicitParameter | Variable | implicits.ImplicitVariable

)
axis1=0
axis2=1
Returns:
Operation

Examples

>>> import gamspy as gp
>>> import numpy as np
>>> m = gp.Container()
>>> identity = np.eye(3, 3)
>>> mat = gp.Parameter(m, name="mat", domain=gp.math.dim([3, 3]), records=identity, uels_on_axes=True)
>>> sc = gp.Parameter(m, name="sc", domain=[])
>>> sc[...] = gp.math.trace(mat)
>>> int(sc.toDense())
3
gamspy.math.vector_norm(x: Parameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation, ord: float | int = 2, dim: list[int] | list[Set | Alias] | None = None) Operation | Expression[source]#

Returns the vector norm of the provided vector x. If ord is not an even integer, absolute value is used which requires DNLP.

Parameters:
xParameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation
ord: int | float
dim: list[int] | list[Set | Alias], optional
Returns:
Expression | Operation

Examples

>>> import gamspy as gp
>>> import math
>>> m = gp.Container()
>>> i = gp.Set(m, name="i", records=["i1", "i2"])
>>> vec = gp.Parameter(m, "vec", domain=[i], records=[("i1", 3), ("i2", 4)])
>>> vlen = gp.Parameter(m, "vlen", domain=[])
>>> vlen[...] = gp.math.vector_norm(vec)
>>> math.isclose(vlen.toValue(), 5, rel_tol=1e-4)
True
gamspy.math.dim(dims: list[int]) Dim[source]#

Returns an array where each element corresponds to a set where the dimension of the set is equal to the element in dims. If same dimension size used, then next free alias will be returned. Symbols are generated once the Dim object is passed to a constructor that supports it.

Parameters:
dims: list[int]
Returns:
Dim

Examples

>>> import gamspy as gp
>>> import math
>>> m = gp.Container()
>>> a = gp.math.dim([10, 20]) # nothing generated yet
>>> a
Dim(dims=[10, 20])
>>> par = gp.Parameter(m, name="par", domain=a) # now two sets are generated
>>> par.domain
[Set(name=DenseDim10_1, domain=['*']), Set(name=DenseDim20_1, domain=['*'])]
>>> par2 = gp.Parameter(m, name="par2", domain=a) # same 2 sets are used
>>> par2.domain
[Set(name=DenseDim10_1, domain=['*']), Set(name=DenseDim20_1, domain=['*'])]
class gamspy.math.Dim(dims: 'list[int]')[source]#

Bases: object

dims: list[int]#
gamspy.math.next_alias(symbol: Alias | Set) Alias[source]#

Provided the set or alias, it returns the next alias. If it is not found, it creates the alias. This function is mainly for matrix multiplication conflict resolution but it might be helpful in the cases where you need to generate many aliases from a set.

Parameters:
symbolSet | Alias
Returns:
Alias

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, name="i", records=["i1", "i2", "i3"])
>>> j = gp.math.next_alias(i)
>>> j.name
'AliasOfi_2'
>>> k = gp.math._generate_dims(m, [10])[0]
>>> k.name
'DenseDim10_1'
>>> k2 = gp.math.next_alias(k)
>>> k2.name
'DenseDim10_2'
gamspy.math.abs(x: int | float | Symbol) Expression[source]#

Absolute value of x (i.e. |x|)

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import abs
>>> m = Container()
>>> a = Parameter(m, "a", records=-3.8)
>>> b = Parameter(m, "b")
>>> b[...] = abs(a)
>>> b.toValue()
np.float64(3.8)
gamspy.math.ceil(x: int | float | Symbol) Expression[source]#

The smallest integer greater than or equal to x (i.e. ceil(4.1) returns 5)

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import ceil
>>> m = Container()
>>> a = Parameter(m, "a", records=3.2)
>>> b = Parameter(m, "b")
>>> b[...] = ceil(a)
>>> b.toValue()
np.float64(4.0)
gamspy.math.dist(x1: int | float | Symbol, x2: int | float | Symbol) Expression[source]#

Euclidean or L-2 Norm: sqrt(x1^2 + x2^2 + ... + xn^2)

Returns:
Expression
Raises:
Exception

In case both x1 and x2 are not a tuple or none.

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import dist
>>> m = Container()
>>> a = Parameter(m, "a", records=210)
>>> b = Parameter(m, "b")
>>> b[...] = dist(a, 100)
gamspy.math.div(dividend: int | float | Symbol, divisor: int | float | Symbol) Expression[source]#

Dividing operation, Error if the divisor is 0. To avoid the error, div0 can be used instead.

Parameters:
dividendint | float | Symbol
divisorint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import div
>>> m = Container()
>>> a = Parameter(m, "a", records=210)
>>> b = Parameter(m, "b")
>>> b[...] = div(a, 3)
>>> b.toValue()
np.float64(70.0)
gamspy.math.div0(dividend: int | float | Symbol, divisor: int | float | Symbol) Expression[source]#

Dividing operation, returns 1e+299 if the divisor is 0

Parameters:
dividendint | float | Symbol
divisorint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import div0
>>> m = Container()
>>> a = Parameter(m, "a", records=210)
>>> b = Parameter(m, "b")
>>> b[...] = div0(a, 0)
>>> b.toValue()
np.float64(1e+299)
gamspy.math.factorial(x: int) Expression[source]#

Factorial of x: x!

Parameters:
xint
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import factorial
>>> m = Container()
>>> b = Parameter(m, "b")
>>> b[...] = factorial(2)
gamspy.math.floor(x: int | float | Symbol) Expression[source]#

The greatest integer less than or equal to x (i.e. floor(4.9) returns 4)

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import floor
>>> m = Container()
>>> a = Parameter(m, "a", records=3.9)
>>> b = Parameter(m, "b")
>>> b[...] = floor(a)
>>> b.toValue()
np.float64(3.0)
gamspy.math.fractional(x: int | float | Symbol) Expression[source]#

Returns the fractional part of x (i.e. fractional(3.9) returns 0.9)

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import fractional
>>> m = Container()
>>> a = Parameter(m, "a", records=3.9)
>>> b = Parameter(m, "b")
>>> b[...] = fractional(a)
>>> b.toValue()
np.float64(0.8999999999999999)
gamspy.math.Min(*values) Expression[source]#

Minimum value of the values, where the number of values may vary.

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import Min
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", -2), ("i2", 0.3), ("i3", 2)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = Min(a[i], 1)
>>> b.toList()
[('i1', -2.0), ('i2', 0.3), ('i3', 1.0)]
gamspy.math.Max(*values) Expression[source]#

Maximum value of the values, where the number of values may vary.

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import Max
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 2), ("i2", 0.3), ("i3", 2.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = Max(a[i], 1)
>>> b.toList()
[('i1', 2.0), ('i2', 1.0), ('i3', 2.5)]
gamspy.math.mod(x: float | Symbol, y: float | Symbol) Expression[source]#

Remainder of x divided by y (i.e. mod(10, 3) returns 1)

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import mod
>>> m = Container()
>>> a = Parameter(m, "a", records=200)
>>> b = Parameter(m, "b")
>>> b[...] = mod(a, 3)
>>> b.toValue()
np.float64(2.0)
gamspy.math.Round(x: float | Symbol, num_decimals: int = 0) Expression[source]#

Round x to num_decimals decimal places (i.e. Round(3.14159, 2) returns 3.14)

Parameters:
xfloat | Symbol
num_decimalsint, optional
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import Round, div
>>> m = Container()
>>> a = Parameter(m, "a", records=200)
>>> b = Parameter(m, "b")
>>> b[...] = Round(div(a, 3), 2)
>>> b.toValue()
np.float64(66.67)
gamspy.math.sign(x: Symbol) Expression[source]#

Sign of x returns 1 if x > 0, -1 if x < 0, and 0 if x = 0

Parameters:
xSymbol
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import sign
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 2), ("i2", -5.4), ("i3", 0)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = sign(a[i])
>>> b.toList()
[('i1', 1.0), ('i2', -1.0)]
gamspy.math.binomial(n: int | float | Symbol, k: int | float | Symbol) Expression[source]#

(Generalized) Binomial coefficient for n > -1 and -1 < k < n + 1

Parameters:
nint | float | Symbol
kint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import binomial
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> p = Parameter(m, "p", domain=i, records=[("i1", 0.3), ("i2", 0.8), ("i3", 0.45)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = binomial(75, p[i])
gamspy.math.centropy(x: int | float | Symbol, y: int | float | Symbol, z: float = 1e-20) Expression[source]#

Cross-entropy: x.ln((x + z) / (y + z)) for x, y > 0 and z >= 0

Parameters:
xfloat | Symbol
yfloat | Symbol
zfloat, optional
Returns:
Expression
Raises:
ValueError

if z is smaller than 0

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import centropy
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> x = Parameter(m, "x", domain=i, records=[("i1", 0.3), ("i2", 8), ("i3", 45)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = centropy(2.8, x[i])
>>> b.toList()
[('i1', 6.254058220219863), ('i2', -2.939501948596297), ('i3', -7.775720603249651)]
gamspy.math.normal(mean: int | float, dev: int | float) Expression[source]#

Generate a random number from the normal distribution with mean mean and standard deviation dev

Parameters:
meanint | float
devint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import normal
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> x = Parameter(m, "x", domain=i, records=[("i1", 30), ("i2", 8), ("i3", 45)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = normal(x[i], 5)
>>> b.toList()
[('i1', 28.433285357057226), ('i2', 9.6383740411321), ('i3', 47.3177939118135)]
gamspy.math.uniform(lower_bound: float | Expression, upper_bound: float | Expression) Expression[source]#

Generates a random number from the uniform distribution between lower_bound and higher_bound

Parameters:
lower_boundfloat
upper_boundfloat
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import uniform
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> x = Parameter(m, "x", domain=i, records=[("i1", 30), ("i2", 8), ("i3", 45)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = uniform(x[i], 50)
>>> b.toList()
[('i1', 33.43494264), ('i2', 43.417201736), ('i3', 47.75187678)]
gamspy.math.uniformInt(lower_bound: int | float, upper_bound: int | float) Expression[source]#

Generates an integer random number from the discrete uniform distribution whose outcomes are the integers between lower_bound and higher_bound

Parameters:
lower_boundint | float
upper_boundint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import uniformInt
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> x = Parameter(m, "x", domain=i, records=[("i1", 30), ("i2", 8), ("i3", 45)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = uniformInt(x[i], 50)
>>> b.toList()
[('i1', 33.0), ('i2', 44.0), ('i3', 48.0)]
gamspy.math.cv_power(base: float, exponent: float | Symbol) Expression[source]#

Real power (i.e. base ^ exponent where base >= 0; error for base < 0)

Parameters:
basefloat
exponentfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import cv_power
>>> m = Container()
>>> a = Parameter(m, "a", records=4)
>>> b = Parameter(m, "b")
>>> b[...] = cv_power(3, a)
gamspy.math.rpower(base: float | Symbol | Operable, exponent: float | Symbol)[source]#

Returns x^y for x > 0 and also for x = 0 and restricted values of y (Error if x < 0)

Parameters:
basefloat | Symbol
exponentfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter, Set
>>> from gamspy.math import rpower
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 3.8)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = rpower(a[i], 3)
>>> b.toList()
[('i1', 54.87199999999999)]
gamspy.math.sign_power(base: float | Symbol, exponent: float)[source]#

Signed power: sign(base) * |base|^exponent, for exponent > 0

Parameters:
basefloat | Symbol
exponentfloat
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter, Set
>>> from gamspy.math import sign_power
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 3.8)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = sign_power(a[i], 5)
>>> b.toList()
[('i1', 792.3516799999994)]
gamspy.math.sllog10(x: int | float | Symbol, S: int | float = 1e-150) Expression[source]#

Smooth (linear) logarithm base 10

Parameters:
xint | float | Symbol
Sint | float, by default 1.0e-150
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import sllog10
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = sllog10(a)
>>> b.toValue()
np.float64(0.5797835966168101)
gamspy.math.sqlog10(x: int | float | Symbol, S: int | float = 1e-150) Expression[source]#

Smooth (quadratic) logarithm base 10

Parameters:
xint | float | Symbol
Sint | float, by default 1.0e-150
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import sqlog10
>>> m = Container()
>>> a = Parameter(m, "a", records=3.8)
>>> b = Parameter(m, "b")
>>> b[...] = sqlog10(a)
>>> b.toValue()
np.float64(0.5797835966168101)
gamspy.math.vc_power(base: float | Symbol, exponent: float | Symbol)[source]#

Returns x^y for x >= 0 (error for x < 0)

Parameters:
basefloat | Symbol
exponentfloat | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import vc_power
>>> m = Container()
>>> a = Parameter(m, "a", records=4)
>>> b = Parameter(m, "b")
>>> b[...] = vc_power(a, 3)
gamspy.math.slexp(x: int | float | Symbol, S: int | float = 150) Expression[source]#

Smooth (linear) exponential where S <= 150. (Default S = 150)

Parameters:
xint | float | Symbol
Sint | float, by default 150
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import slexp
>>> m = Container()
>>> a = Parameter(m, "a", records=3)
>>> b = Parameter(m, "b")
>>> b[...] = slexp(a)
>>> b.toValue()
np.float64(20.085536923187668)
gamspy.math.sqexp(x: int | float | Symbol, S: int | float = 150) Expression[source]#

Smooth (quadratic) exponential where S <= 150. (Default S = 150)

Parameters:
xint | float | Symbol
Sint | float, by default 150
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import sqexp
>>> m = Container()
>>> a = Parameter(m, "a", records=3)
>>> b = Parameter(m, "b")
>>> b[...] = sqexp(a)
>>> b.toValue()
np.float64(20.085536923187668)
gamspy.math.truncate(x: int | float | Symbol) Expression[source]#

Returns the integer part of x (i.e. truncate(3.9) returns 3)

Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import truncate
>>> m = Container()
>>> a = Parameter(m, "a", records=3.9)
>>> b = Parameter(m, "b")
>>> b[...] = truncate(a)
>>> b.toValue()
np.float64(3.0)
gamspy.math.ifthen(condition: Expression, yes_return: float | Expression, no_return: float | Expression) Expression[source]#

If the logical condition is true, the function returns yes_return, else it returns no_return

Parameters:
conditionExpression
yes_returnfloat | Expression
no_returnfloat | Expression
Returns:
Expression

Examples

>>> from gamspy.math import ifthen
>>> import gamspy as gp
>>> m = gp.Container()
>>> tt = gp.Parameter(m, "tt", records=2)
>>> y = gp.Parameter(m, "y", records=2)
>>> x = ifthen(tt == 2, 3, 4 + y)
gamspy.math.beta(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Beta function: B(x, y) = gamma(x) * gamma(y) / gamma(x + y) = (x-1)! * (y-1)! / (x + y - 1)!

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import beta
>>> m = Container()
>>> a = Parameter(m, "a", records=3)
>>> b = Parameter(m, "b")
>>> b[...] = beta(a, 1)
>>> b.toValue()
np.float64(0.3333333333333333)
gamspy.math.regularized_beta(x: int | float, y: int | float, z: int | float) Expression[source]#

Regularized Beta Function, See MathWorld

Parameters:
xint | float
yint | float
zint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import regularized_beta
>>> m = Container()
>>> a = Parameter(m, "a", records=3)
>>> b = Parameter(m, "b")
>>> b[...] = regularized_beta(0.5, a, 1)
>>> b.toValue()
np.float64(0.12500000000000003)
gamspy.math.gamma(x: int | float | Symbol) Expression[source]#

Gamma function: gamma(x) = (x-1)!

Parameters:
xint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import gamma
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 7), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = gamma(a[i])
>>> b.toList()
[('i1', 6.0), ('i2', 720.0), ('i3', 1.772453850905516)]
gamspy.math.regularized_gamma(x: int | float, a: int | float) Expression[source]#

Lower Incomplete Regularized Gamma function, See MathWorld

Parameters:
xint | float
aint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import regularized_gamma
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 1), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = regularized_gamma(0.5, a[i])
>>> b.toList()
[('i1', 0.001751622556290824), ('i2', 0.3934693402873665), ('i3', 0.6826894921370857)]
gamspy.math.entropy(x: int | float | Symbol) Expression[source]#

Entropy function: -x*ln(x) where x >= 0

Parameters:
xint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import entropy
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 1), ("i2", 0.8), ("i3", 15)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = entropy(a[i])
>>> b.toList()
[('i2', 0.17851484105136778), ('i3', -40.62075301653315)]
gamspy.math.lse_max(*xs) Expression[source]#

Smoothed Max via the Logarithm of the Sum of Exponentials: ln(exp(x1) + exp(x2) + ... + exp(xn))

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import lse_max
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 10), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = lse_max(a[i], 5)
>>> b.toList()
[('i1', 5.313261687518223), ('i2', 10.006715348489118), ('i3', 5.011047744848594)]
gamspy.math.lse_max_sc(t, *xs) Expression[source]#

Scaled smoothed Max via the Logarithm of the Sum of Exponentials: lse_max_sc(T,x) = lse_max(Tx)/T

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import lse_max_sc
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 100), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = lse_max_sc(7.5, a[i], 10.5)
>>> b.toList()
[('i1', 10.50000153604837), ('i2', 10.5), ('i3', 10.902826555965506)]
gamspy.math.lse_min(*xs) Expression[source]#

Smoothed Min via the Logarithm of the Sum of Exponentials: -ln(exp(-x1) + exp(-x2) + ... + exp(-xn))

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import lse_min
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 10), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = lse_min(a[i], 5)
>>> b.toList()
[('i1', 3.686738312481777), ('i2', 4.993284651510882), ('i3', 0.4889522551514062)]
gamspy.math.lse_min_sc(t, *xs) Expression[source]#

Scaled smoothed Min via the Logarithm of the Sum of Exponentials: lse_min_sc(T,x) = lse_min(Tx)/T

Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import lse_min_sc
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 100), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = lse_min_sc(7.5, a[i], 10.5)
>>> b.toList()
[('i1', 4.0), ('i2', 10.5), ('i3', 0.5)]
gamspy.math.ncp_cm(x: Symbol, y: Symbol, z: float | int) Expression[source]#

Chen-Mangasarian smoothing: x - z*ln(1 + exp((x-y)/z))

Parameters:
xSymbol
ySymbol
zint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import ncp_cm
>>> m = Container()
>>> y = Parameter(m, "y", records=2)
>>> b = Parameter(m, "b")
>>> b[...] = ncp_cm(1, y, 0.5)
>>> b.toValue()
np.float64(0.9365359944785137)
gamspy.math.ncp_f(x: Symbol, y: Symbol, z: int | float = 0) Expression[source]#

Fisher-Burmeister smoothing: sqrt(x^2 + y^2 + 2z) - x - y where z >= 0 (default z = 0)

Parameters:
xSymbol
ySymbol
zint | float, optional
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import ncp_f
>>> m = Container()
>>> y = Parameter(m, "y", records=2)
>>> b = Parameter(m, "b")
>>> b[...] = ncp_f(1, y, 0.5)
>>> b.toValue()
np.float64(-0.5505102572168221)
gamspy.math.ncpVUpow(r: Symbol, s: Symbol, mu: int | float = 0) Expression[source]#

NCP Veelken-Ulbrich (smoothed min(r,s))

Parameters:
rSymbol
sSymbol
muint | float, optional
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import ncpVUpow
>>> m = Container()
>>> y = Parameter(m, "y", records=2)
>>> b = Parameter(m, "b")
>>> b[...] = ncpVUpow(1, y, 0.5)
>>> b.toValue()
np.float64(1.0)
gamspy.math.ncpVUsin(r: Symbol, s: Symbol, mu: int | float = 0) Expression[source]#

NCP Veelken-Ulbrich (smoothed min(r,s))

Parameters:
rSymbol
sSymbol
muint | float, optional
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import ncpVUsin
>>> m = Container()
>>> y = Parameter(m, "y", records=2)
>>> b = Parameter(m, "b")
>>> b[...] = ncpVUsin(1, y, 0.5)
>>> b.toValue()
np.float64(1.0)
gamspy.math.poly(x, *args) Expression[source]#

Polynomial function: p(x) = A[0] + A[1]*x + A[2]*x^2 + ... + A[n-1]*x^(n-1)

Returns:
Expression
Raises:
ValidationError

If the number of arguments (args) is less than 3 or if any of args is not an integer or a float.

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import poly
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", 10), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = poly(a[i], 15, 3, 4)
>>> b.toList()
[('i1', 91.0), ('i2', 445.0), ('i3', 17.5)]
gamspy.math.rand_binomial(n: int | float, p: int | float) Expression[source]#

Generate a random number from the binomial distribution, where n is the number of trials and p the probability of success for each trial

Parameters:
nint | float
pint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import rand_binomial
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> p = Parameter(m, "p", domain=i, records=[("i1", 0.3), ("i2", 0.8), ("i3", 0.45)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = rand_binomial(75, p[i])
>>> b.toList()
[('i1', 21.0), ('i2', 63.0), ('i3', 25.0)]
gamspy.math.rand_linear(low: int | float, slope: int | float, high: int | float) Expression[source]#

Generate a random number between low and high with linear distribution. slope must be less than 2 / (high - low) and greater than 0

Parameters:
lowint | float
slopeint | float
highint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import rand_linear
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> s = Parameter(m, "s", domain=i, records=[("i1", 0.03), ("i2", 0.008), ("i3", 0.04)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = rand_linear(75, s[i], 125)
>>> b.toList()
[('i1', 78.22119203430918), ('i2', 87.65662570307367), ('i3', 80.24583337516547)]
gamspy.math.rand_triangle(low: int | float, mid: int | float, high: int | float) Expression[source]#

Generate a random number between low and high with triangular distribution. mid is the most probable number.

Parameters:
lowint | float
midint | float
highint | float
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import rand_triangle
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> s = Parameter(m, "s", domain=i, records=[("i1", 103), ("i2", 80), ("i3", 115)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = rand_triangle(75, s[i], 125)
>>> b.toList()
[('i1', 90.50632080153123), ('i2', 106.22102486822031), ('i3', 108.17756338250294)]
gamspy.math.slrec(x: int | float | Symbol, S: int | float = 1e-10) Expression[source]#

Smooth (linear) reciprocal, where S >= 1e-10. (Default S = 1e-10)

Parameters:
xint | float | Symbol
Sint | float, by default 1e-10
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import slrec
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 1), ("i2", 0.8), ("i3", 15)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = slrec(a[i])
>>> b.toList()
[('i1', 1.0), ('i2', 1.25), ('i3', 0.06666666666666667)]
gamspy.math.sqrec(x: int | float | Symbol, S: int | float = 1e-10) Expression[source]#

Smooth (quadratic) reciprocal, where S >= 1e-10. (Default S = 1e-10)

Parameters:
xint | float | Symbol
Sint | float, by default 1e-10
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import sqrec
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 1), ("i2", 0.8), ("i3", 15)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = sqrec(a[i])
>>> b.toList()
[('i1', 1.0), ('i2', 1.25), ('i3', 0.06666666666666667)]
gamspy.math.errorf(x: int | float | Symbol) Expression[source]#

Integral of the standard normal distribution from negative infinity to x

Parameters:
xint, float, Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import errorf
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", -2.5), ("i2", 0.8), ("i3", 1.7)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = errorf(a[i])
>>> b.toList()
[('i1', 0.0062096653257761375), ('i2', 0.7881446014166034), ('i3', 0.955434537241457)]
gamspy.math.same_as(self: Set | Alias, other: Set | Alias | str) Expression[source]#

Evaluates to true if this set is identical to the given set or alias, false otherwise.

Parameters:
otherSet | Alias
Returns:
Expression

Examples

>>> import gamspy as gp
>>> from gamspy.math import same_as
>>> m = gp.Container()
>>> i = gp.Set(m, name="i", records=["seattle", "san-diego"])
>>> j = gp.Set(m, name="j", records=["new-york", "seattle"])
>>> attr = gp.Parameter(m, "attr", domain = [i, j])
>>> attr[i,j]  =  same_as(i, j)
>>> attr.records.values.tolist()
[['seattle', 'seattle', 1.0]]
gamspy.math.sigmoid(x: int | float | Symbol) Expression[source]#

Sigmoid of x (i.e. 1 / (1 + exp(-x)))

Parameters:
xint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Set, Parameter
>>> from gamspy.math import sigmoid
>>> m = Container()
>>> i = Set(m, name="i", records=["i1", "i2", "i3"])
>>> a = Parameter(m, "a", domain=i, records=[("i1", 4), ("i2", -1), ("i3", 0.5)])
>>> b = Parameter(m, "b", domain=i)
>>> b[i] = sigmoid(a[i])
>>> b.toList()
[('i1', 0.9820137900379085), ('i2', 0.2689414213699951), ('i3', 0.6224593312018546)]
gamspy.math.bool_and(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff both x and y are true

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import bool_and
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = bool_and(a > 10, b < 5)
>>> c.toValue()
np.float64(0.0)
gamspy.math.bool_eqv(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns false iff exactly one argument is false

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import bool_eqv
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = bool_eqv(a > 10, b < 5)
>>> c.toValue()
np.float64(0.0)
gamspy.math.bool_imp(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x is false or y is true

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import bool_imp
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = bool_imp(a < 10, b > 5)
>>> c.toValue()
np.float64(1.0)
gamspy.math.bool_not(x: int | float | Symbol) Expression[source]#

Returns true iff x is false

Parameters:
xint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import bool_not
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = bool_not(a > 10)
>>> c.toValue()
np.float64(0.0)
gamspy.math.bool_or(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x is true or y is true

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import bool_or
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = bool_or(a > 15, b < 5)
>>> c.toValue()
np.float64(0.0)
gamspy.math.bool_xor(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff exactly one argument is false

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import bool_xor
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = bool_xor(a < 15, b > 5)
>>> c.toValue()
np.float64(0.0)
gamspy.math.rel_eq(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x == y

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import rel_eq
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = rel_eq(a, b)
>>> c.toValue()
np.float64(0.0)
gamspy.math.rel_ge(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x >= y

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import rel_ge
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = rel_ge(a, b)
>>> c.toValue()
np.float64(1.0)
gamspy.math.rel_gt(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x > y

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import rel_gt
>>> m = Container()
>>> a = Parameter(m, "a", records=7)
>>> b = Parameter(m, "b", records=7)
>>> c = Parameter(m, "c")
>>> c[...] = rel_gt(a, b)
>>> c.toValue()
np.float64(0.0)
gamspy.math.rel_le(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x <= y

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import rel_le
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=11)
>>> c = Parameter(m, "c")
>>> c[...] = rel_le(a, b)
>>> c.toValue()
np.float64(0.0)
gamspy.math.rel_lt(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x < y

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import rel_lt
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=17)
>>> c = Parameter(m, "c")
>>> c[...] = rel_lt(a, b)
>>> c.toValue()
np.float64(1.0)
gamspy.math.rel_ne(x: int | float | Symbol, y: int | float | Symbol) Expression[source]#

Returns true iff x != y

Parameters:
xint | float | Symbol
yint | float | Symbol
Returns:
Expression

Examples

>>> from gamspy import Container, Parameter
>>> from gamspy.math import rel_ne
>>> m = Container()
>>> a = Parameter(m, "a", records=12)
>>> b = Parameter(m, "b", records=12)
>>> c = Parameter(m, "c")
>>> c[...] = rel_ne(a, b)
>>> c.toValue()
np.float64(0.0)
gamspy.math.relu_with_sos1_var(x: Parameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation, return_slack_var: bool = False)[source]#

Implements the ReLU activation function using SOS1 variables. The ReLU function is defined as ReLU(x) = max(x, 0). This implementation generates one SOS1 type variable which is necessary to represent the mathematical relationship and this SOS1 variable contains the activation variable and slack variable.

Unlike relu_with_binary_var, this function does not require lower and upper bounds for the formulation. It is claimed that when providing tight bounds is not straightforward, using relu_with_sos1_var might perform better than relu_with_binary_var, as the relaxation of relu_with_binary_var can be weak.

Usage of SOS1 variables require MIP or MINLP and a solver that supports SOS1 variables. Main intended use case of this function is embedding the trained neural network into MIP models, we do not suggest using it in training since you would need a MINLP solver that support SOS1 variables.

Returns activation variable if return_slack_var is False, otherwise returns activation and slack variable in order. Since activation variable and slack variable are the same variable only separated by the last domain this function returns ImplicitVariable instead of Variable.

Based on paper: PySCIPOpt-ML: Embedding trained machine learning models into mixed-integer programs.

Parameters:
xParameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation
return_slack_var: bool
Returns:
implicits.ImplicitVariable | tuple[implicits.ImplicitVariable, implicits.ImplicitVariable]

Examples

>>> from gamspy import Container, Variable, Set
>>> from gamspy.math.activation import relu_with_sos1_var
>>> m = Container()
>>> i = Set(m, "i", records=range(3))
>>> x = Variable(m, "x", domain=[i])
>>> y = relu_with_sos1_var(x)
>>> y, s = relu_with_sos1_var(x, return_slack_var=True)
>>> y.domain # implicit activation variable has the same domain
[Set(name=i, domain=['*'])]
>>> s.domain # implicit slack variable has the same domain as well
[Set(name=i, domain=['*'])]
>>> y.name == s.name # In the background that y and s are parts of the same variable
True
>>> id(y.parent) == id(s.parent)
True
gamspy.math.relu_with_binary_var(x: Parameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation, default_lb: float = -1000000, default_ub: float = 1000000, return_binary_var: bool = False)[source]#

Implements the ReLU activation function using binary variables. The ReLU function is defined as ReLU(x) = max(x, 0). This implementation generates one binary variable and one positive variable. The binary variable is necessary to represent the mathematical relationship, while the positive variable serves as the activation variable. Both the binary and positive variables share the same domain as the input.

The formulation of this function requires having lower and upper bounds for the input x. This function utilizes the bounds from the variables if provided. If not, it defaults to the bounds defined by default_lb and default_ub. Providing tighter and correct bounds can enhance the quality of linear relaxations.

Returns activation variable if return_binary_var is False, otherwise returns activation and binary variable in order.

Adapted from OMLT

Parameters:
xParameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation
default_ubfloat
default_lbfloat
return_binary_var: bool
Returns:
Variable | tuple[Variable, Variable]

Examples

>>> from gamspy import Container, Variable, Set
>>> from gamspy.math.activation import relu_with_binary_var
>>> m = Container()
>>> i = Set(m, "i", records=range(3))
>>> x = Variable(m, "x", domain=[i])
>>> y = relu_with_binary_var(x)
>>> y.type
'positive'
>>> y, b = relu_with_binary_var(x, return_binary_var=True)
>>> b.type
'binary'
>>> y.domain # i many activation variables
[Set(name=i, domain=['*'])]
>>> b.domain # i many binary variables
[Set(name=i, domain=['*'])]
gamspy.math.relu_with_complementarity_var(x: Parameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation)[source]#

Implements the ReLU activation function using complementarity conditions. The ReLU function is defined as ReLU(x) = max(x, 0). This implementation generates one positive variable, which serves as the activation variable. The activation variable shares the same domain as the input. Unlike relu_with_binary_var, this function does not require lower and upper bounds for the formulation.

Returns the activation variable.

Adapted from OMLT

Parameters:
xParameter | Variable | implicits.ImplicitParameter | implicits.ImplicitVariable | Expression | Operation
Returns:
Variable

Examples

>>> from gamspy import Container, Variable, Set
>>> from gamspy.math.activation import relu_with_complementarity_var
>>> m = Container()
>>> i = Set(m, "i", records=range(3))
>>> x = Variable(m, "x", domain=[i])
>>> y = relu_with_complementarity_var(x)
>>> y.type
'positive'
gamspy.math.log_softmax(x: Variable, dim: int = -1)[source]#

Implements the log_softmax activation function. This function strictly requires a GAMSPy Variable, y = log_softmax(x). The dim parameter specifies the index of the softmax dimension. If not provided, it calculates log_softmax for the last dimension. This function is preferred over the softmax function because, when the softmax dimension has 20 or fewer elements, it uses the lse_max (log-sum-exp) intrinsic function for improved numerical stability which usually leads to faster solve times.

To learn more about Log-Sum-Exp trick .

This function is usually combined with Negative Log Likelihood loss for classification problems.

Returns the activation variable.

Parameters:
xVariable
dimint
Returns:
Variable

Examples

>>> from gamspy import Container, Variable
>>> from gamspy.math import dim
>>> from gamspy.math.activation import log_softmax
>>> m = Container()
>>> x = Variable(m, "x", domain=dim([500, 10]))
>>> y = log_softmax(x) # uses LSE because 10 <= 20
>>> y.domain
[Set(name=DenseDim500_1, domain=['*']), Set(name=DenseDim10_1, domain=['*'])]
>>> y2 = log_softmax(x, dim=0) # cannot use LSE because 500 > 20
gamspy.math.softmax(x: Variable, dim: int = -1)[source]#

Implements the softmax activation function. This function strictly requires a GAMSPy Variable, y = softmax(x). The dim parameter specifies the index of the softmax dimension. If not provided, the softmax is calculated for the last dimension. This function is implemented for completeness; however, in many cases, you can use log_softmax for better numerical stability.

Use log_softmax if you need to take the logarithm of the softmax function.

Returns the activation variable.

Parameters:
xVariable
dimint
Returns:
Variable

Examples

>>> from gamspy import Container, Variable
>>> from gamspy.math import dim
>>> from gamspy.math.activation import softmax
>>> m = Container()
>>> x = Variable(m, "x", domain=dim([500, 10]))
>>> y = softmax(x)
>>> y.domain
[Set(name=DenseDim500_1, domain=['*']), Set(name=DenseDim10_1, domain=['*'])]