-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor common utilities to resolve matrix coefficients [breaking]
This is an attempt to simplify the workflow, by avoiding having python calling lua, calling python, calling lua, etc.. The motivation is also to work around this bug: scoder/lupa#243 With such purpose in mind, I now use Lua for the generation of the assignments of the matrix coefficients, rather than Python. This change breaks the existing code generation backends. Also, the code now requires the language-specifics to have an explicit function to turn a sympy expression into text. This is used instead of the naive call to Sympy's __str__, to give the backend more chances for customization. For example, powers in Sympy (Python) are written with '**', which is not a valid operator in other languages like C. Therefore, simply using __str__ would cause invalid code to be generated.
- Loading branch information
1 parent
770364f
commit a10cca0
Showing
2 changed files
with
83 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
-- This module contains common functions to generate the code for the | ||
-- assignments of matrix coefficients. | ||
-- | ||
-- These are the functions that actually transform the Sympy expressions (every | ||
-- matrix coefficient is in general a Sympy expression) into text. | ||
-- | ||
-- The generators of this module require to be given matrices where all the | ||
-- referenced symbols (variables, parametes, constants) are "resolved". See the | ||
-- Python module `ctgen.common.SymbolicCoefficientsResolver`. | ||
-- | ||
-- With resolved matrices, turning the Sympy expressions to text is usually as | ||
-- simple as tostring-ing the expression. | ||
|
||
|
||
local common = ctgen__common | ||
|
||
|
||
--- Factory for the functions that generate the assignments of the matrix | ||
--- coefficients. | ||
-- | ||
-- | ||
local getMatrixSpecificGenerators = function(lang, matrixMetadata, resolvedMatrix) | ||
-- resolvedMatrix must have all reference to symbols resolved | ||
local ret = {} | ||
local coefficient = common.py_matrix_coeff(resolvedMatrix) | ||
|
||
ret.constantCoefficientsAssignments = function(matrixVarName) | ||
local it, inv, ctrl = python.iter(matrixMetadata.constantCoefficients) | ||
return function() | ||
local r,c = it(inv, ctrl) -- run the iterator once | ||
ctrl = r | ||
if r ~= nil then | ||
-- we do not generate assignments of zeros, assuming the matrix is initialized | ||
local value = coefficient(r, c) | ||
if value ~= 0.0 then | ||
-- even though it is constant, it may still be an expression | ||
-- hence we use the dedicated toString function | ||
local value_expr = lang.sympyExpressionToString( value ) | ||
return lang.matrixAssignment(matrixVarName,r,c,value_expr) | ||
end | ||
end | ||
return nil | ||
end, inv, ctrl | ||
end | ||
|
||
ret.variableCoefficientsAssignments = function(matrixVarName) | ||
local it, inv, ctrl = python.iter(matrixMetadata.variableCoefficients) | ||
return function() | ||
local r,c = it(inv, ctrl) -- run the iterator once | ||
ctrl = r | ||
if r ~= nil then | ||
local value = lang.sympyExpressionToString( coefficient(r,c) ) | ||
return lang.matrixAssignment(matrixVarName,r,c, value) | ||
end | ||
return nil | ||
end, inv, ctrl | ||
end | ||
|
||
return ret | ||
end | ||
|
||
|
||
-- The global table with the functions of this module | ||
ctgen__matrix_coeff_assignments = { | ||
getMatrixSpecificGenerators = getMatrixSpecificGenerators, | ||
} | ||
|
||
return ctgen__matrix_coeff_assignments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters