-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from rzyu45/dev
Support of user defined functions
- Loading branch information
Showing
8 changed files
with
242 additions
and
127 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
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
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
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
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
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
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,47 @@ | ||
""" | ||
Test the user defined modules. | ||
""" | ||
|
||
|
||
def test_udm(): | ||
|
||
from Solverz import Model, Var, Eqn, made_numerical, MulVarFunc | ||
import numpy as np | ||
|
||
class Min(MulVarFunc): | ||
arglength = 2 | ||
|
||
def fdiff(self, argindex=1): | ||
if argindex == 1: | ||
return dMindx(*self.args) | ||
elif argindex == 2: | ||
return dMindy(*self.args) | ||
|
||
def _numpycode(self, printer, **kwargs): | ||
return (f'myfunc.Min' + r'(' + | ||
', '.join([printer._print(arg, **kwargs) for arg in self.args]) + r')') | ||
|
||
class dMindx(MulVarFunc): | ||
arglength = 2 | ||
|
||
def _numpycode(self, printer, **kwargs): | ||
return (f'myfunc.dMindx' + r'(' + | ||
', '.join([printer._print(arg, **kwargs) for arg in self.args]) + r')') | ||
|
||
class dMindy(MulVarFunc): | ||
arglength = 2 | ||
|
||
def _numpycode(self, printer, **kwargs): | ||
return (f'myfunc.dMindy' + r'(' + | ||
', '.join([printer._print(arg, **kwargs) for arg in self.args]) + r')') | ||
|
||
m = Model() | ||
m.x = Var('x', [1, 2]) | ||
m.y = Var('y', [3, 4]) | ||
m.f = Eqn('f', Min(m.x, m.y)) | ||
sae, y0 = m.create_instance() | ||
ae = made_numerical(sae, y0, sparse=True) | ||
np.testing.assert_allclose(ae.F(y0, ae.p), np.array([1.0, 2.0])) | ||
np.testing.assert_allclose(ae.J(y0, ae.p).toarray(), np.array([[1., 0., 0., 0.], | ||
[0., 1., 0., 0.]])) | ||
|
Oops, something went wrong.