Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign a value to the Constant object #149

Open
ratnania opened this issue Mar 6, 2024 · 1 comment
Open

Assign a value to the Constant object #149

ratnania opened this issue Mar 6, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@ratnania
Copy link
Contributor

ratnania commented Mar 6, 2024

Up to now, we can create a Constant object which is later considered as a free-variable, for example

kappa = Constant('kappa', is_real=True)

We would also like to allow the assignnment of a value to the Constant object, e.g.

kappa = Constant('kappa', 1.1)

In this case, kappa will not be treated as a free-variable by TerminalExpr, which will use subs to replace occurrences of the symbol kappa by its value.

@ratnania ratnania added the enhancement New feature or request label Mar 6, 2024
@ratnania ratnania self-assigned this Mar 6, 2024
@yguclu yguclu transferred this issue from pyccel/IGA-Python Mar 6, 2024
@yguclu
Copy link
Member

yguclu commented Mar 6, 2024

After a long discussion @ratnania and I propose to have the three classes ScalarConstant, VectorConstant, and MatrixConstant.

Objects of these types can be created through a unique Constant constructor, which will select the correct type based on the shape argument (which is 0 by default, corresponding to a scalar).

If the user does not provide a value to the constructor, the dtype must be specified and the resulting object is treated as an undefined constant of a specific type. If value is specified the dtype is calculated from it. If both value and dtype are present, the values are cast to the required type if this does not cause a conversion error.

For vectors and matrices the shape argument should always be provided for clarity. If the given value has a shape inconsistent with the one provided, an error will be raised.

The signature of the constructor is:

Constant(name, *, shape=0, dtype=None, value=None)

And here are some examples:

i = Constant('i', value=5)        # integer constant with value 5
r = Constant('r', dtype=float)    # undefined real constant
c = Constant('c', dtype=complex)  # undefined complex constant

w = Constant('w', shape=2, dtype=float, value=[3, 7])  # 2-vector of floats with value [3.0, 7.0]
n = Constant('n', shape=3, dtype=int)                  # undefined 3-vector of integers

M = Constant('M', shape=(2, 2), value=[[1, 2], [3, 4]]) # 2x2 matrix of integers with value [[1, 2], [3, 4]]
A = Constant('A', shape=(3, 3), dtype=complex)          # undefined 3x3 matrix of complex numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants