X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=mjo%2Feja%2Feja_algebra.py;h=c76863264539fa75b5f0cbbca68066424f8d6b7f;hb=6201a7cc616538014fd51384cf96177730b11d2d;hp=c1d66ddaaea7f01f86b4f84065cfa22eb5be1590;hpb=a3ae7a0a3258c97384068178a9556ca3544c36f2;p=sage.d.git diff --git a/mjo/eja/eja_algebra.py b/mjo/eja/eja_algebra.py index c1d66dd..c768632 100644 --- a/mjo/eja/eja_algebra.py +++ b/mjo/eja/eja_algebra.py @@ -5,7 +5,6 @@ are used in optimization, and have some additional nice methods beyond what can be supported in a general Jordan Algebra. """ -#from sage.algebras.finite_dimensional_algebras.finite_dimensional_algebra import FiniteDimensionalAlgebra from sage.algebras.quatalg.quaternion_algebra import QuaternionAlgebra from sage.categories.finite_dimensional_algebras_with_basis import FiniteDimensionalAlgebrasWithBasis from sage.combinat.free_module import CombinatorialFreeModule @@ -18,7 +17,6 @@ from sage.rings.number_field.number_field import QuadraticField from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.rational_field import QQ from sage.structure.element import is_Matrix -from sage.structure.category_object import normalize_names from mjo.eja.eja_element import FiniteDimensionalEuclideanJordanAlgebraElement from mjo.eja.eja_utils import _mat2vec @@ -61,6 +59,68 @@ class FiniteDimensionalEuclideanJordanAlgebra(CombinatorialFreeModule): self.print_options(bracket='') + def _element_constructor_(self, elt): + """ + Construct an element of this algebra from its natural + representation. + + This gets called only after the parent element _call_ method + fails to find a coercion for the argument. + + SETUP:: + + sage: from mjo.eja.eja_algebra import (JordanSpinEJA, + ....: RealCartesianProductEJA, + ....: RealSymmetricEJA) + + EXAMPLES: + + The identity in `S^n` is converted to the identity in the EJA:: + + sage: J = RealSymmetricEJA(3) + sage: I = matrix.identity(QQ,3) + sage: J(I) == J.one() + True + + This skew-symmetric matrix can't be represented in the EJA:: + + sage: J = RealSymmetricEJA(3) + sage: A = matrix(QQ,3, lambda i,j: i-j) + sage: J(A) + Traceback (most recent call last): + ... + ArithmeticError: vector is not in free module + + TESTS: + + Ensure that we can convert any element of the two non-matrix + simple algebras (whose natural representations are their usual + vector representations) back and forth faithfully:: + + sage: set_random_seed() + sage: J = RealCartesianProductEJA(5) + sage: x = J.random_element() + sage: J(x.to_vector().column()) == x + True + sage: J = JordanSpinEJA(5) + sage: x = J.random_element() + sage: J(x.to_vector().column()) == x + True + + """ + natural_basis = self.natural_basis() + if elt not in natural_basis[0].matrix_space(): + raise ValueError("not a naturally-represented algebra element") + + # Thanks for nothing! Matrix spaces aren't vector + # spaces in Sage, so we have to figure out its + # natural-basis coordinates ourselves. + V = VectorSpace(elt.base_ring(), elt.nrows()*elt.ncols()) + W = V.span_of_basis( _mat2vec(s) for s in natural_basis ) + coords = W.coordinate_vector(_mat2vec(elt)) + return self.from_vector(coords) + + def _repr_(self): """ Return a string representation of ``self``.