1 | initial version |
It seems you are confusing calls to SparseMatrixProduct
and SparseMatrixPower
. Indeed, SparseMatrixPower(A,A)
is meaningless (the second argument should be an integer not a matrix).
2 | No.2 Revision |
It seems you are confusing calls to SparseMatrixProduct
and SparseMatrixPower
. Indeed, SparseMatrixPower(A,A)
is meaningless (the second argument should be an integer not a matrix).
When p
is odd, your code try to make two steps at once (see the (p-1)/2
). To understand the algorithm that is behind your code, I would suggest to separate the add/multiply (p- > p-1
) part and the double/squaring (p->p/2
) part as follows :
sage: def SparseMatrixPower(A,p):
....: if p == 1:
....: return(A)
....: elif Mod(p,2):
....: return SparseMatrixProduct(A,SparseMatrixPower(A,p-1))
....: else:
....: return SparseMatrixPower(SparseMatrixProduct(A,A), p / 2)
That said, if you plan to play a lot with twose objects, I would suggest to define a class for them and define a __mul__
method to define the product. Then Sage has generic double-and-add (or multiply-and-square) to automatically define the power, see for example https://doc.sagemath.org/html/en/reference/rings_standard/sage/arith/power.html
3 | No.3 Revision |
It seems you are confusing calls to SparseMatrixProduct
and SparseMatrixPower
. Indeed, SparseMatrixPower(A,A)
is meaningless (the second argument should be an integer not a matrix).
When p
is odd, your code try to make two steps at once (see the (p-1)/2
). To understand the algorithm that is behind your code, I would suggest to separate the add/multiply (
) part and the double/squaring (p- > p -> p-1
) part as follows :p->p/2p -> p/2
sage: def SparseMatrixPower(A,p):
....: if p == 1:
....: return(A)
....: elif Mod(p,2):
....: return SparseMatrixProduct(A,SparseMatrixPower(A,p-1))
....: else:
....: return SparseMatrixPower(SparseMatrixProduct(A,A), p / 2)
That said, if you plan to play a lot with twose objects, I would suggest to define a class for them and define a __mul__
method to define the product. Then Sage has generic double-and-add (or multiply-and-square) to automatically define the power, see for example https://doc.sagemath.org/html/en/reference/rings_standard/sage/arith/power.html
4 | No.4 Revision |
It seems you are confusing calls to SparseMatrixProduct
and SparseMatrixPower
. Indeed, SparseMatrixPower(A,A)
is meaningless (the second argument should be an integer not a matrix).
When p
is odd, your code try to make two steps at once (see the (p-1)/2
). To understand the algorithm that is behind your code, I would suggest to separate the add/multiply (p -> p-1
) part and the double/squaring (p -> p/2
) part as follows :
sage: def SparseMatrixPower(A,p):
SparseMatrixPower(A, p):
....: if p == 1:
....: return(A)
....: elif Mod(p,2):
Mod(p, 2):
....: return SparseMatrixProduct(A,SparseMatrixPower(A,p-1))
SparseMatrixProduct(A, SparseMatrixPower(A, p-1))
....: else:
....: return SparseMatrixPower(SparseMatrixProduct(A,A), p / 2)
SparseMatrixPower(SparseMatrixProduct(A, A), p/2)
That said, if you plan to play a lot with twose objects, I would suggest to define a class for them and define a __mul__
method to define the product. Then Sage has generic double-and-add (or multiply-and-square) to automatically define the power, see for example https://doc.sagemath.org/html/en/reference/rings_standard/sage/arith/power.html