Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,14 +631,23 @@ class I_(Predefined, SympyObject):
summary_text = "imaginary unit number Sqrt[-1]"
python_equivalent = 1j

def evaluate(self, evaluation: Evaluation):
return Complex(Integer0, Integer1)

def is_constant(self) -> bool:
"""The value and evaluation of this object can never change."""
return True

def to_sympy(self, symb, **kwargs):
return self.sympy_obj
@property
def is_literal(self):
return True

def evaluate(self, evaluation: Evaluation):
return Complex(Integer0, Integer1)
@property
def value(self) -> complex:
return complex(0, 1)

def to_sympy(self, expr, **kwargs):
return self.sympy_obj


class Im(SympyFunction):
Expand Down
1 change: 1 addition & 0 deletions mathics/builtin/numbers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def eval_N(self, precision, evaluation):
return self.get_constant(precision, evaluation)

def is_constant(self) -> bool:
"""The value and evaluation of this object can never change."""
return True

def get_constant(
Expand Down
12 changes: 2 additions & 10 deletions mathics/builtin/tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
of any rank can be handled.
"""


from mathics.core.atoms import Integer
from mathics.core.attributes import A_FLAT, A_ONE_IDENTITY, A_PROTECTED
from mathics.core.builtin import Builtin, InfixOperator
Expand All @@ -28,6 +27,7 @@
eval_Inner,
eval_LeviCivitaTensor,
eval_Outer,
eval_Transpose2D,
get_dimensions,
)

Expand Down Expand Up @@ -378,15 +378,7 @@ class Transpose(Builtin):

def eval(self, m, evaluation: Evaluation):
"Transpose[m_?MatrixQ]"

result = []
for row_index, row in enumerate(m.elements):
for col_index, item in enumerate(row.elements):
if row_index == 0:
result.append([item])
else:
result[col_index].append(item)
return ListExpression(*[ListExpression(*row) for row in result])
return eval_Transpose2D(m)


class ConjugateTranspose(Builtin):
Expand Down
4 changes: 4 additions & 0 deletions mathics/core/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ def __new__(cls, real, imag):
f"Argument 'image' must be an Integer, Real, or Rational type; is {imag}."
)

# Note: for the below test, imag.value == 0 catches more
# reals. In particular, MachineReals that have an imaginary
# value of floating point 0.0. But MachineReal 0.0 is "approximate 0",
# not exactly 0. So "Complex[0., 0.]" is "0. + 0." and not "0."
if imag.sameQ(Integer0):
return real

Expand Down
13 changes: 8 additions & 5 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,23 @@ def __init__(self, *args, **kwargs):
self.sympy_name = strip_context(self.get_name()).lower()
self.mathics_to_sympy[self.__class__.__name__] = self.sympy_name

def is_constant(self) -> bool:
return False

def get_sympy_names(self) -> List[str]:
if self.sympy_name:
return [self.sympy_name]
return []

def to_sympy(self, expr=None, **kwargs):
raise NotImplementedError
def is_constant(self) -> bool:
"""Returns true if the value of evaluation of this object can
never change.
"""
return False

def from_sympy(self, elements: Tuple[BaseElement, ...]) -> Expression:
raise NotImplementedError

def to_sympy(self, expr=None, **kwargs):
raise NotImplementedError


# This has to come before MPMathFunction
class SympyFunction(SympyObject):
Expand Down
Loading
Loading