Main

!!!! Arrays in Python GEKKO

Multi-dimensional arrays are defined in [[https://gekko.readthedocs.io/en/latest/model_methods.html|Python GEKKO]] with the m.Array() function or with list comprehensions.

(:source lang=python:)

from gekko import GEKKO

m = GEKKO()

ni = 3 # number of rows

nj = 2 # number of columns

# best method: use m.Array function

x = m.Array(m.Var,(ni,nj))

m.Equations([x[i][j]==i*j+1 for i in range(ni) for j in range(nj)])

# another way: list comprehensions

y = [[m.Var() for j in range(nj)] for i in range(ni)]

for i in range(ni):

for j in range(nj):

m.Equation(x[i][j]**2==y[i][j])

m.solve()

print('x:')

print(x)

print('y=x**2:')

print(y)

(:sourceend:)

An additional example is a matrix summation where there are two indices of the parameter matrix ''p''.
~~ Intermediates~~

z[1] = p[1]

z[2] = z[1] + p[2]

z[3] = z[2] + p[3]

z[4] = z[3] + p[4]

z[5] = z[4] + p[5]

End Intermediates

~~!! Arrays~~

!!! Higher dimensional arrays

Arrays with more than one dimension are allowed. The array indices are separated by commas as in ''var[i,j,k...]''.

## APMonitor Summation with Vectors

## Main.Arrays History

Show minor edits - Show changes to output

Added lines 174-177:

# summation

z = m.Var()

m.Equation(z==sum([sum([x[i][j] for i in range(ni)]) for j in range(nj)]))

z = m.Var()

m.Equation(z==sum([sum([x[i][j] for i in range(ni)]) for j in range(nj)]))

Added lines 184-185:

print('z')

print(z.value)

print(z.value)

Added lines 150-180:

!!!! Arrays in Python GEKKO

Multi-dimensional arrays are defined in [[https://gekko.readthedocs.io/en/latest/model_methods.html|Python GEKKO]] with the m.Array() function or with list comprehensions.

(:source lang=python:)

from gekko import GEKKO

m = GEKKO()

ni = 3 # number of rows

nj = 2 # number of columns

# best method: use m.Array function

x = m.Array(m.Var,(ni,nj))

m.Equations([x[i][j]==i*j+1 for i in range(ni) for j in range(nj)])

# another way: list comprehensions

y = [[m.Var() for j in range(nj)] for i in range(ni)]

for i in range(ni):

for j in range(nj):

m.Equation(x[i][j]**2==y[i][j])

m.solve()

print('x:')

print(x)

print('y=x**2:')

print(y)

(:sourceend:)

Added lines 118-119:

An additional example is a matrix summation where there are two indices of the parameter matrix ''p''.

Changed lines 28-29 from:

!!! ~~Example~~

to:

!!! Examples

Changed line 33 from:

! Summation with arrays

to:

! Method #1: Summation with arrays

Changed lines 59-60 from:

! ~~Summation without arrays~~

Model array

Model array

to:

! Method #2: Summation with the sum object

Objects

z = sum(5)

End Objects

Connections

p[1:n] = z.x[1:n]

y = z.y

End Connections

Model

Constants

n = 5

End Constants

Objects

z = sum(5)

End Objects

Connections

p[1:n] = z.x[1:n]

y = z.y

End Connections

Model

Constants

n = 5

End Constants

Changed lines 75-79 from:

p[1~~] = 1~~

p[2] = 1

p[3] = 1

p[4] = 1

p[5] = 1

p[2] = 1

p[3] = 1

p[4] = 1

p[5

to:

p[1:n] = 1

Added line 79:

y

Deleted lines 82-89:

z[1] = p[1]

z[2] = z[1] + p[2]

z[3] = z[2] + p[3]

z[4] = z[3] + p[4]

z[5] = z[4] + p[5]

End Intermediates

Changed line 84 from:

sum = ~~z[5]~~

to:

sum = y

Added lines 88-119:

(:cellnr:)

! Method #3: Summation without arrays

Model array

Parameters

p[1] = 1

p[2] = 1

p[3] = 1

p[4] = 1

p[5] = 1

End Parameters

Variables

sum

End Variables

Intermediates

z[1] = p[1]

z[2] = z[1] + p[2]

z[3] = z[2] + p[3]

z[4] = z[3] + p[4]

z[5] = z[4] + p[5]

End Intermediates

Equations

sum = z[5]

End Equations

End Model

(:tableend:)

(:table border=1 width=100% align=left bgcolor=#EEEEEE cellspacing=0:)

! Method #3: Summation without arrays

Model array

Parameters

p[1] = 1

p[2] = 1

p[3] = 1

p[4] = 1

p[5] = 1

End Parameters

Variables

sum

End Variables

Intermediates

z[1] = p[1]

z[2] = z[1] + p[2]

z[3] = z[2] + p[3]

z[4] = z[3] + p[4]

z[5] = z[4] + p[5]

End Intermediates

Equations

sum = z[5]

End Equations

End Model

(:tableend:)

(:table border=1 width=100% align=left bgcolor=#EEEEEE cellspacing=0:)

Changed line 9 from:

Arrays with more than one dimension are allowed. The array indices are separated by ~~commas~~ as~~ in ''var[i,j,k...]'' or~~ ''var[i][j][k]''. For operations on matrices, the precedence of operations is determined by the number of colons separating the vector indices. Matrix elements with fewer colon separators are executed first. For example, a set of 24 intermediate variables posed as:

to:

Arrays with more than one dimension are allowed. The array indices are separated by brackets as ''var[i][j][k]''. For operations on matrices, the precedence of operations is determined by the number of colons separating the vector indices. Matrix elements with fewer colon separators are executed first. For example, a set of 24 intermediate variables posed as:

Changed lines 2-3 from:

(:summation, array, vector, sum, matrix, algebraic, modeling language:)

(:description Summation of vectors and matrices in APMonitor:)

(:description Summation of vectors and matrices in APMonitor:)

to:

(:keywords summation, array, vector, sum, matrix, algebraic, modeling language:)

(:description Summation of vectors and matrices in APMonitor :)

(:description Summation of vectors and matrices in APMonitor :)

Added lines 1-4:

(:title APMonitor Summation with Vectors:)

(:summation, array, vector, sum, matrix, algebraic, modeling language:)

(:description Summation of vectors and matrices in APMonitor:)

(:summation, array, vector, sum, matrix, algebraic, modeling language:)

(:description Summation of vectors and matrices in APMonitor:)

Deleted lines 0-1:

Changed line 26 from:

(:table border=1 width=~~50~~% align=left bgcolor=#EEEEEE cellspacing=0:)

to:

(:table border=1 width=100% align=left bgcolor=#EEEEEE cellspacing=0:)

Changed lines 7-20 from:

Arrays with more than one dimension are allowed. The array indices are separated by commas as in ''var[i,j,k...]''.

to:

Arrays with more than one dimension are allowed. The array indices are separated by commas as in ''var[i,j,k...]'' or ''var[i][j][k]''. For operations on matrices, the precedence of operations is determined by the number of colons separating the vector indices. Matrix elements with fewer colon separators are executed first. For example, a set of 24 intermediate variables posed as:

* x[1:2][1::3][1:::4] = 1

results in the following set of equations:

* x[1][1][1] = 1

* x[2][1][1] = 1

* x[1][2][1] = 1

* x[2][2][1] = 1

* x[1][3][1] = 1

* x[2][3][1] = 1

* x[1][1][2] = 1

* etc...

* x[1:2][1::3][1:::4] = 1

results in the following set of equations:

* x[1][1][1] = 1

* x[2][1][1] = 1

* x[1][2][1] = 1

* x[2][2][1] = 1

* x[1][3][1] = 1

* x[2][3][1] = 1

* x[1][1][2] = 1

* etc...

Added lines 68-94:

End Equations

End Model

(:cellnr:)

! Matrix Summation

Model

Parameters

p[1:10][1::5] = 1

End Parameters

Variables

x

End Variables

Intermediates

! sum the rows

n[0][1:5] = 0

n[1:10][1::5] = n[0:9][1::5] + p[1:10][1::5]

! sum the columns that are summation of rows

m[0] = 0

m[1:5] = m[0:4] + n[10][1:5]

End Intermediates

Equations

! solution = 50

x = m[5]

End Model

(:cellnr:)

! Matrix Summation

Model

Parameters

p[1:10][1::5] = 1

End Parameters

Variables

x

End Variables

Intermediates

! sum the rows

n[0][1:5] = 0

n[1:10][1::5] = n[0:9][1::5] + p[1:10][1::5]

! sum the columns that are summation of rows

m[0] = 0

m[1:5] = m[0:4] + n[10][1:5]

End Intermediates

Equations

! solution = 50

x = m[5]

Added lines 20-23:

Constants

n = 5

End Constants

n = 5

End Constants

Changed line 25 from:

p[1:~~5~~] = 1

to:

p[1:n] = 1

Changed line 34 from:

z[2:~~5~~] = z[1:~~4~~] + p[2:~~5~~]

to:

z[2:n] = z[1:n-1] + p[2:n]

Changed line 38 from:

sum = z[~~5~~]

to:

sum = z[n]

Added lines 4-7:

!!! Higher dimensional arrays

Arrays with more than one dimension are allowed. The array indices are separated by commas as in ''var[i,j,k...]''.

Changed lines 3-7 from:

Variable or object arrays are defined by square brackets with a range of integers and separated by a colon~~. Arrays may be used to define multiple equations or operations on one line. Any~~ line ~~with an array is processed sequentially from the lowest to~~ the ~~highest~~ index.

to:

Variable or object arrays are defined by square brackets with a range of integers and separated by a colon as ''variable[index 1:index 2]''. Arrays may be used to define multiple equations or connections on one line. Any line with an array is processed sequentially from the lowest to the highest index. The model parser creates and processes the arrays as if they were written sequential in non-array form as shown in the example.

!!! Array Index Consistency

When processing the arrays, the parser checks for array size consistency. An error with an appropriate message is returned if the vector indeces are of different dimension.

!!! Array Index Consistency

When processing the arrays, the parser checks for array size consistency. An error with an appropriate message is returned if the vector indeces are of different dimension.

Added lines 7-59:

(:table border=1 width=50% align=left bgcolor=#EEEEEE cellspacing=0:)

(:cellnr:)

! Summation with arrays

Model array

Parameters

p[1:5] = 1

End Parameters

Variables

sum

End Variables

Intermediates

z[1] = p[1]

z[2:5] = z[1:4] + p[2:5]

End Intermediates

Equations

sum = z[5]

End Equations

End Model

(:cellnr:)

! Summation without arrays

Model array

Parameters

p[1] = 1

p[2] = 1

p[3] = 1

p[4] = 1

p[5] = 1

End Parameters

Variables

sum

End Variables

Intermediates

z[1] = p[1]

z[2] = z[1] + p[2]

z[3] = z[2] + p[3]

z[4] = z[3] + p[4]

z[5] = z[4] + p[5]

End Intermediates

Equations

sum = z[5]

End Equations

End Model

(:tableend:)

(:cellnr:)

! Summation with arrays

Model array

Parameters

p[1:5] = 1

End Parameters

Variables

sum

End Variables

Intermediates

z[1] = p[1]

z[2:5] = z[1:4] + p[2:5]

End Intermediates

Equations

sum = z[5]

End Equations

End Model

(:cellnr:)

! Summation without arrays

Model array

Parameters

p[1] = 1

p[2] = 1

p[3] = 1

p[4] = 1

p[5] = 1

End Parameters

Variables

sum

End Variables

Intermediates

z[1] = p[1]

z[2] = z[1] + p[2]

z[3] = z[2] + p[3]

z[4] = z[3] + p[4]

z[5] = z[4] + p[5]

End Intermediates

Equations

sum = z[5]

End Equations

End Model

(:tableend:)

Added lines 1-6:

!! Arrays

Variable or object arrays are defined by square brackets with a range of integers and separated by a colon. Arrays may be used to define multiple equations or operations on one line. Any line with an array is processed sequentially from the lowest to the highest index.

!!! Example

Variable or object arrays are defined by square brackets with a range of integers and separated by a colon. Arrays may be used to define multiple equations or operations on one line. Any line with an array is processed sequentially from the lowest to the highest index.

!!! Example