+
+def _scale(x, alpha):
+ r"""
+ Scale the vector, matrix, or cartesian-product-of-those-things
+ ``x`` by ``alpha``.
+
+ This works around the inability to scale certain elements of
+ Cartesian product spaces, as reported in
+
+ https://trac.sagemath.org/ticket/31435
+
+ ..WARNING:
+
+ This will do the wrong thing if you feed it a tuple or list.
+
+ SETUP::
+
+ sage: from mjo.eja.eja_utils import _scale
+
+ EXAMPLES::
+
+ sage: v = vector(QQ, (1,2,3))
+ sage: _scale(v,2)
+ (2, 4, 6)
+ sage: m = matrix(QQ, [[1,2],[3,4]])
+ sage: M = cartesian_product([m.parent(), m.parent()])
+ sage: _scale(M((m,m)), 2)
+ ([2 4]
+ [6 8], [2 4]
+ [6 8])
+
+ """
+ if hasattr(x, 'cartesian_factors'):
+ P = x.parent()
+ return P(tuple( _scale(x_i, alpha)
+ for x_i in x.cartesian_factors() ))
+ else:
+ return x*alpha
+
+
+def _all2list(x):
+ r"""
+ Flatten a vector, matrix, or cartesian product of those things
+ into a long list.
+
+ EXAMPLES::
+
+ sage: from mjo.eja.eja_utils import _all2list
+ sage: V1 = VectorSpace(QQ,2)
+ sage: V2 = MatrixSpace(QQ,2)
+ sage: x1 = V1([1,1])
+ sage: x2 = V1([1,-1])
+ sage: y1 = V2.one()
+ sage: y2 = V2([0,1,1,0])
+ sage: _all2list((x1,y1))
+ [1, 1, 1, 0, 0, 1]
+ sage: _all2list((x2,y2))
+ [1, -1, 0, 1, 1, 0]
+ sage: M = cartesian_product([V1,V2])
+ sage: _all2list(M((x1,y1)))
+ [1, 1, 1, 0, 0, 1]
+ sage: _all2list(M((x2,y2)))
+ [1, -1, 0, 1, 1, 0]
+
+ """
+ if hasattr(x, 'list'):
+ # Easy case...
+ return x.list()
+ else:
+ # But what if it's a tuple or something else? This has to
+ # handle cartesian products of cartesian products, too; that's
+ # why it's recursive.
+ return sum( map(_all2list,x), [] )