-
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.
- Loading branch information
Marco F
committed
Sep 19, 2023
1 parent
19b3b92
commit f5f536c
Showing
2 changed files
with
69 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,54 @@ | ||
-- This module contains common functions to generate the code for the | ||
-- assignments of matrix coefficients. | ||
|
||
local common = 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 | ||
local value = coefficient(r, c) | ||
if value ~= 0.0 then | ||
-- we do not generate assignments of zeros, assuming the matrix is initialized | ||
return lang.matrixAssignment(matrixVarName,r,c,tostring(value)) | ||
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 = tostring( 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