-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#131 - Matrix power via square decomposition, v2 #134
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,11 +67,12 @@ function IntervalMatrixPower(M::IntervalMatrix{T}) where {T} | |
return IntervalMatrixPower(M, M, 1) | ||
end | ||
|
||
function IntervalMatrixPower(M::IntervalMatrix{T}, k::Int) where {T} | ||
function IntervalMatrixPower(M::IntervalMatrix{T}, k::Int; | ||
algorithm::String="power") where {T} | ||
@assert k >= 1 "matrix powers must be positive" | ||
pow = IntervalMatrixPower(M, M, 1) | ||
@inbounds for i in 1:(k-1) | ||
increment!(pow) | ||
increment!(pow; algorithm=algorithm) | ||
end | ||
return pow | ||
end | ||
|
@@ -93,6 +94,7 @@ Increment a matrix power in-place (i.e., storing the result in `pow`). | |
* `"multiply"` -- fast computation using `*` from the previous result | ||
* `"power"` -- recomputation using `^` | ||
* `"intersect"` -- combination of `"multiply"` and `"power"` | ||
* `"sqrt"` -- decompose `k = a² + b` | ||
|
||
### Output | ||
|
||
|
@@ -113,6 +115,8 @@ function increment!(pow::IntervalMatrixPower; algorithm::String="intersect") | |
pow.Mᵏ = _eval_power(pow) | ||
elseif algorithm == "intersect" | ||
pow.Mᵏ = _eval_intersect(pow) | ||
elseif algorithm == "sqrt" | ||
pow.Mᵏ = _eval_sqrt(pow) | ||
else | ||
throw(ArgumentError("algorithm $algorithm is not available; choose " * | ||
"from 'multiply', 'power', 'intersect'")) | ||
|
@@ -164,6 +168,20 @@ function _eval_intersect(pow::IntervalMatrixPower) | |
return intersect(_eval_multiply(pow), _eval_power(pow)) | ||
end | ||
|
||
function _eval_sqrt(pow::IntervalMatrixPower; algorithm::String="power") | ||
# decompose k = a² + b with a, b being integers | ||
k = pow.k | ||
a = floor(Int, sqrt(k)) | ||
b = k - a^2 | ||
|
||
# recursively compute M^a and M^b | ||
Mᵏ = square(get(IntervalMatrixPower(pow.M, a; algorithm=algorithm))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is line 178 the same as EDIT: This is now #138. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My question still is why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because it is not equivalent. We do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, damn, your version was the correct one: |
||
if b > 0 | ||
Mᵏ *= get(IntervalMatrixPower(pow.M, b; algorithm=algorithm)) | ||
end | ||
return Mᵏ | ||
end | ||
|
||
""" | ||
base(pow::IntervalMatrixPower) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should algorithm be a field of the type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also thinking about that. It is more general to let you choose at every step, but we could at least have a fallback algorithm defined in the type maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first sight it looks better to me if the algorithm is in the type, since it is a property of how to get to
M^k
, and also how to get higher powers. (Of course i'm assuming that there is no mixed algorithm, but the "mixed" algorithm can be an algorithm by itself).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, we can revisit this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and no. If the mixed algorithm follows a strict pattern, it can be added as another algorithm. But adding a new algorithm for any pattern is not sustainable. Furthermore, you can imagine patterns that depend on user knowledge (like "next I need more precision") that you cannot express in a static algorithm. So I conclude that a
String
as a field is not sufficient. If we make the algorithm a field, it should be its own struct<:IntervalMatrixPowerAlgorithm
that you can influence from outside.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #139.