Using CombinatorialFreeModule

Could I please have some help on using CombinatorialFreeModule? I have looked at the documentation and the only examples are free modules on a finite set. I have designed my own class and I now want to work with linear combinations of instances of the class. I have been told that one issue is that instances have to be immutable (so they can be hashed?). The attributes of my class are all integers and tuples (of integers). However I am not sure if this is sufficient.

Let's say I have defined a class G whose attributes are all integers and tuples. Then the idea would be say M = CombinatorialFreeModule(QQ,G) to define the free module on instances of G with rational coefficients. Then if g,h,k are instances of G I would hope to be able to put say a = (2/3)g + 4h and b = (-3)h + 2k be able to add these and multiply by rational numbers.

For example, a simple (but inefficient) way to calculate the chromatic polynomial of a graph would be to use contraction/deletion. This starts with a graph and finishes with a polynomial but the intermediate stages are formal linear combinations of graphs. Similarly the skein relation approach to link polynomials has intermediate stages which are formal linear combinations of link projections.

edit retag close merge delete

Hi BWW, could you post some example code of what you want, or what breaks?

( 2010-09-01 11:48:16 +0200 )edit

Sort by » oldest newest most voted

Here is a minimal example that may do what you want:

class Foo(SageObject):
# This class defines what a 'Foo' is and what can be done with it.
def __init__(self, i):
self.i = i

def _repr_(self):
return str(self.i)

class Foos(Parent, UniqueRepresentation):
# This class represents the set of all 'Foo's
def __contains__(self, f):
return isinstance(f, Foo)

Element = Foo


Here is how to use this with CombinatorialFreeModule:

sage: F = Foos()
sage: f = Foo(1)
sage: g = Foo(2)
sage: f
1
sage: g
2
sage: f + g
TypeError: unsupported operand type(s) for +: 'Foo' and 'Foo'
sage: M = CombinatorialFreeModule(QQ, F)
sage: a = M.monomial(f)
sage: b = M.monomial(g)
sage: a + b
B[2] + B[1]
sage: 2*a
2*B[1]

more

As in Jason's example, it's almost certainly a good idea to use SageObject as a base class from which to inherit. Other structures in Sage will interact better with "class Foo(SageObject): ..." than with "class Foo(): ...".

( 2010-09-02 20:28:01 +0200 )edit

Jason, This seems to do what I want. Thanks for your help.

( 2010-09-03 05:29:19 +0200 )edit

You can give CombinatorialFreeModule a Family as a basis. For example:

sage: CombinatorialFreeModule(QQ, Family(NN))
Free module generated by
Family (Non negative integer semiring)
over Rational Field

sage: CombinatorialFreeModule(QQ, Family(NN, lambda d: Partitions(d)))
Free module generated by
Lazy family (<lambda>(i))_{i in Non negative integer semiring}
over Rational Field


Look at the implementation of the example of an algebra with basis in sage/categories/examples/algebras_with_basis.py (docs here) and trac #9280 for examples of this.

more

By the way, you could also look at the implementation of group algebras as an example of a Hopf algebra with basis, in sage/categories/examples/hopf_algebras_with_basis.py.

( 2010-09-02 17:54:09 +0200 )edit
1

( 2022-01-19 21:45:59 +0200 )edit

The problem may be simply that you need to give CombinatorialFreeModule a list of basis elements:

sage: P = PolynomialRing(QQ,'x')
sage: Q = PolynomialRing(QQ,'y')
sage: R = PolynomialRing(QQ,'z')
sage: C = CombinatorialFreeModule(QQ,[P,Q,R])
sage: C
Free module generated by {Univariate Polynomial Ring in x over Rational Field, Univariate Polynomial Ring in y over Rational Field, Univariate Polynomial Ring in z over Rational Field} over Rational Field
sage: B = [x for x in C.basis()]
sage: q = 3/5*B[0] + 1/2*B[1]
sage: q
3/5*B[Univariate Polynomial Ring in x over Rational Field] + 1/2*B[Univariate Polynomial Ring in y over Rational Field]
sage: q in C
True

sage: P.rename('p')
sage: Q.rename('q')
sage: R.rename('r')
sage: C = CombinatorialFreeModule(QQ,[P,Q,R])
sage: C
Free module generated by {p, q, r} over Rational Field


Is this something like what you're trying to do?

You've probably seen it already, but here's the documentation for CombinatorialFreeModule. If this still isn't doing what you want, you could try implementing addition and scalar multiplication for your class; this is described in the "How To Implement" section of the Coercion documentation.

more

The examples in the documentation give finite lists. I require the (potentially) infinite set of all instances of a class.

( 2010-09-01 13:53:00 +0200 )edit

My class does not have addition and multiplication. This is purely formal (as in, for example, the group algebra of a group which may be infinite).

( 2010-09-01 13:54:19 +0200 )edit

I agree that Parents and Elements will come into this. I have tried reading the documentation on Categories and I have simply not grasped the idea.

( 2010-09-01 14:48:17 +0200 )edit

It sounds like you're not so interested in the free module itself as the ability to do arithmetic over QQ with instances of your class--is that right? If so, I think the Coercion documentation is really the right place for you. For example . . .

( 2010-09-01 16:19:13 +0200 )edit

if you define a method _add_, then you will automatically be able to take instances p and q and have p+q be a new instance of your class. And I strongly suggest skipping to the main example, which is a basic implementation of Localization rings:

( 2010-09-01 16:23:06 +0200 )edit