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.

Comments

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(): ...".

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.

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 . . .

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: