Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 Ind
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 

10 Tools
 10.1 Mathematical Operations
 10.2 List Operations
 10.3 Helper Functions
 10.4 Python Integration

10 Tools

10.1 Mathematical Operations

10.1-1 Relu
‣ Relu( a )( operation )

Returns: a float

The ReLU (Rectified Linear Unit) activation function. Returns Maximum(a, 0) for a float a.

10.1-2 KroneckerDelta
‣ KroneckerDelta( a, b )( function )

Returns: 0 or 1

The Kronecker delta function. Returns 1 if a equals b, otherwise 0.

10.1-3 MultiplyMatrices
‣ MultiplyMatrices( m_1, mat_1, n_1, m_2, mat_2, n_2 )( function )

Returns: a matrix

Multiply two matrices with explicit dimensions. mat_1 is an m_1 x n_1 matrix, mat_2 is an m_2 x n_2 matrix. Requires n_1 = m_2.

10.2 List Operations

10.2-1 Enumerate
‣ Enumerate( l )( operation )

Returns: a list of pairs

Create enumerated pairs from a list. Returns a list of pairs [i, l[i]] for each element in l.

10.2-2 SplitDenseList
‣ SplitDenseList( l, dims )( operation )

Returns: a list of lists

Split a dense list l according to the dimensions in dims. The sum of dimensions must equal the length of the list.

10.2-3 SplitDenseList
‣ SplitDenseList( l, n )( operation )

Returns: a list of lists

Split a dense list l into sublists of size n. If the length of l is not a multiple of n, the last sublist will contain the remaining elements (i.e., will contain less than n elements).

10.3 Helper Functions

10.3-1 SelectBasedOnCondition
‣ SelectBasedOnCondition( cond, val_true, val_false )( function )

Returns: an element

Select an element based on a condition. If cond is true, returns val_true, otherwise returns val_false.

10.3-2 CallFuncListBasedOnCondition
‣ CallFuncListBasedOnCondition( cond, func_1, func_2, args )( function )

Returns: the result of the called function

Call one of two functions based on a condition. If cond is true, calls func_1, otherwise calls func_2 on the arguments in args. For example, CallFuncListBasedOnCondition( true, x -> x^2, x -> x^3, [ 2 ] ); returns \(4\), while CallFuncListBasedOnCondition( false, x -> x^2, x -> x^3, [ 2 ] ); returns \(8\).

10.4 Python Integration

10.4-1 Diff
‣ Diff( vars, str, i )( operation )

Returns: a function

Compute the partial derivative of an expression with respect to the i-th variable. vars is a list of variable names, str is the expression string. See the example in the Expressions chapter.

10.4-2 LazyDiff
‣ LazyDiff( vars, str, i )( operation )

Returns: a function

Compute the lazy partial derivative of an expression with respect to the i-th variable. vars is a list of variable names, str is the expression string. See the example in the Expressions chapter.

10.4-3 JacobianMatrixUsingPython
‣ JacobianMatrixUsingPython( vars, exps, indices )( operation )

Returns: a matrix of derivative expressions

Compute the Jacobian matrix using Python's SymPy library. vars is a list of variable names, exps is a list of expression strings, indices specifies which variables to differentiate with respect to.

10.4-4 LazyJacobianMatrix
‣ LazyJacobianMatrix( vars, exps, indices )( operation )

Returns: a function

Compute a lazy Jacobian matrix (deferred computation). Returns a function that computes the Jacobian when evaluated.

10.4-5 ScatterPlotUsingPython
‣ ScatterPlotUsingPython( points, labels )( operation )

Returns: the directory containing the plot

Create a scatter plot using Python's matplotlib. points is a list of 2D points, labels is a list of class labels.

10.4-6 SimplifyExpressionUsingPython
‣ SimplifyExpressionUsingPython( vars, exps )( operation )

Returns: a list of simplified expression strings

Simplify expressions using Python's SymPy library. vars is a list of variable names, exps is a list of expression strings.

10.4-7 LaTeXOutputUsingPython
‣ LaTeXOutputUsingPython( vars, exps )( operation )

Returns: a list of LaTeX strings

Convert expressions to LaTeX format using Python's SymPy library. vars is a list of variable names, exps is a list of expression strings.

10.4-8 AsCythonFunction
‣ AsCythonFunction( vars, function_names, functions )( operation )

Returns: a string with instructions to use the compiled functions

Compile functions to Cython for improved performance. vars is a list of lists of variable names (one per function), function_names is a list of function names, functions is a list of function body strings.

gap> dummy_input := CreateContextualVariables( [ "a", "b", "c" ] );
[ a, b, c ]
gap> AssignExpressions( dummy_input );;
#I  MakeReadWriteGlobal: a already read-write
#I  MakeReadWriteGlobal: b already read-write
#I  MakeReadWriteGlobal: c already read-write
gap> e := Sin( a ) + Cos( b );
Sin( a ) + Cos( b )
gap> Diff( e, 1 )( dummy_input );
Cos( a )
gap> LazyDiff( e, 1 )( dummy_input );
Diff( [ "a", "b", "c" ], "(Sin(a))+(Cos(b))", 1 )( [ a, b, c ] )
gap> JacobianMatrixUsingPython( [ a*Cos(b)+Exp(c), a*b*c ], [ 1, 2, 3 ] );
[ [ "Cos(b)", "-a*Sin(b)", "Exp(c)" ], [ "b*c", "a*c", "a*b" ] ]
gap> JacobianMatrix(
>   [ "a", "b", "c" ],
>   [ "a*Cos(b)+Exp(c)", "a*b*c" ],
>   [ 1, 2, 3 ] )(dummy_input);
[ [ Cos(b), (-a)*Sin(b), Exp(c) ], [ b*c, a*c, a*b ] ]
gap> SimplifyExpressionUsingPython(
>   [ "a", "b" ],
>   [ "Sin(a)^2 + Cos(a)^2", "Exp(Log(b))" ] );
[ "1", "b" ]
gap> LaTeXOutputUsingPython( e );
"\\sin{\\left(a \\right)} + \\cos{\\left(b \\right)}"
gap> AsCythonFunction( [[ "x", "y" ], [ "z" ]], ["f", "g"], ["x*y", "Sin(z)"] );;
gap> """
> It will produce output similar to the following lines:
> $ cd /tmp/gaptempdirI6rq3l/
> $ python
> >>> from cython_functions import f, g
> >>> w = [ 2, 3 ] # or any other vector in R^2
> >>> f(w)
> 6.0
> """;;
gap> sigmoid := Expression( [ "x" ], "Exp(x)/(1+Exp(x))" );
Exp( x ) / (1 + Exp( x ))
gap> sigmoid := AsFunction( sigmoid );
function( vec ) ... end
gap> sigmoid( [ 0 ] );
0.5
gap> points := List( 0.1 * [ -20 .. 20 ], x -> [ x, sigmoid( [ x ] ) ] );
[ [ -2., 0.119203 ], [ -1.9, 0.130108 ], [ -1.8, 0.141851 ], [ -1.7, 0.154465 ],
  [ -1.6, 0.167982 ], [ -1.5, 0.182426 ], [ -1.4, 0.197816 ], [ -1.3, 0.214165 ],
  [ -1.2, 0.231475 ], [ -1.1, 0.24974 ], [ -1., 0.268941 ], [ -0.9, 0.28905 ],
  [ -0.8, 0.310026 ], [ -0.7, 0.331812 ],  [ -0.6, 0.354344 ], [ -0.5, 0.377541 ],
  [ -0.4, 0.401312 ], [ -0.3, 0.425557 ], [ -0.2, 0.450166 ], [ -0.1, 0.475021 ],
  [ 0., 0.5 ], [ 0.1, 0.524979 ], [ 0.2, 0.549834 ], [ 0.3, 0.574443 ],
  [ 0.4, 0.598688 ], [ 0.5, 0.622459 ], [ 0.6, 0.645656 ], [ 0.7, 0.668188 ],
  [ 0.8, 0.689974 ], [ 0.9, 0.71095 ], [ 1., 0.731059 ], [ 1.1, 0.75026 ],
  [ 1.2, 0.768525 ], [ 1.3, 0.785835 ], [ 1.4, 0.802184 ],  [ 1.5, 0.817574 ],
  [ 1.6, 0.832018 ], [ 1.7, 0.845535 ], [ 1.8, 0.858149 ], [ 1.9, 0.869892 ],
  [ 2., 0.880797 ] ]
gap> labels := List( points, point -> 0 );;
gap> ScatterPlotUsingPython( points, labels : size := "100", action := "save" );;
 [Top of Book]  [Contents]   [Previous Chapter]   [Next Chapter] 
Goto Chapter: Top 1 2 3 4 5 6 7 8 9 10 Ind

generated by GAPDoc2HTML