Rational matrix division in Julia - julia

In Julia, matrix division of two rational-valued matrices returns a floating-point matrix. How can I obtain a matrix of rational numbers instead?
I can't just use convert(Array{Rational}, A \ b) because of the loss of precision associated with floating-point numbers.

You'd have to implement a pivoted QR factorization algorithm for rational matrices, which is a pretty non-trivial undertaking, although certainly not impossible. Julia uses the LAPACK DGEQP3 function to do this for 64-bit floating-point matrices. Even if you did manage to get a rational QR factorization working, it would be nowhere near as fast as the LAPACK algorithm. So I guess I would ask what you need exact rational matrix division for.
Aside: You may find it more fruitful to ask questions like this on the julia-users list since you will be able to have a conversation about it – this isn't really an "asked and answered" kind of problem.
Update: This now "just works" because a generic pivoted QR now exists in Julia:
julia> A = [rand(1:10)//rand(1:10) for i=1:4, j=1:4]
4x4 Array{Rational{Int64},2}:
5//3 1//2 10//1 8//9
1//1 3//2 6//7 2//3
4//1 8//9 6//7 10//3
7//2 5//2 1//2 5//1
julia> b = collect(1:4)
4-element Array{Int64,1}:
1
2
3
4
julia> c = A\b
4-element Array{Rational{Int64},1}:
42055//62497
61344//62497
-2954//62497
-19635//124994
julia> A*c
4-element Array{Rational{Int64},1}:
1//1
2//1
3//1
4//1
Beware, however, that Rational{Int} are fairly prone to overflow, so you may need to use Rational{Int128} or Rational{BigInt} to avoid overflows. This algorithm is thoroughly generic and works for even more exotic numerical types as well:
julia> using Quaternions # install with Pkg.add("Quaternions")
julia> A = [Quaternion(rand(1:10), rand(1:10), rand(1:10), rand(1:10)) for i=1:4, j=1:4]
4x4 Array{Quaternions.Quaternion{Int64},2}:
4 + 3im + 5jm + 8km 9 + 7im + 10jm + 3km 9 + 3im + 1jm + 7km 8 + 4im + 8jm + 5km
1 + 4im + 2jm + 1km 5 + 4im + 1jm + 4km 1 + 5im + 8jm + 2km 7 + 2im + 5jm + 3km
10 + 1im + 4jm + 4km 2 + 4im + 9jm + 2km 2 + 10im + 4jm + 10km 2 + 3im + 4jm + 8km
7 + 4im + 3jm + 7km 8 + 3im + 5jm + 9km 6 + 5im + 1jm + 3km 10 + 10im + 3jm + 1km
julia> b = collect(1:4)
4-element Array{Int64,1}:
1
2
3
4
julia> c = A\b
4-element Array{Quaternions.Quaternion{Float64},1}:
0.18112 + 0.019288355350921868im - 0.002638049486498678jm + 0.11047233503816825km
0.000578119 + 0.08073035854610844im - 0.023278758601757744jm - 0.16761078955242836km
-0.0501257 - 0.02428891715971441im - 0.11059096793480247jm - 0.059017235311989824km
0.0394953 - 0.16771397199827004im - 0.019823106776170954jm + 0.05251791790026253km
julia> A*c
4-element Array{Quaternions.Quaternion{Float64},1}:
1.0 + 1.1102230246251565e-16im + 0.0jm + 0.0km
2.0 + 2.220446049250313e-16im + 0.0jm + 0.0km
3.0 + 4.440892098500626e-16im - 2.220446049250313e-16jm + 8.881784197001252e-16km
4.0 + 2.220446049250313e-16im - 2.220446049250313e-16jm + 6.661338147750939e-16km
julia> norm(b - A*c)
1.680072297996111e-15
Note that quaternion multiplication is not commutative, which makes this rather interesting.

Related

Prove (2^n - 1) via induction

I have this program that, when run, has prints in this pattern:
a(0) = 0
a(1) = 1
a(2) = 2 + a(1) = 3
a(3) = 3 + a(2) + a(1) = 3 + 3 + 1 = 7
a(4) = 4 + 3 + 3 + 1 = 15
At this point I observe that there is a pattern of it becoming O(2^n - 1). However, I am not sure if this is a valid proof by induction. An idea I had was:
a(n)= n + a(n-1) + a(n-2) + ... + 1 = 2^n - 1
But from here the pattern for the terms are not very clear to me. I know the first term is n (in our code, this is due to a for loop that prints a statement n times), and due to recursion I know I will be summing the previous values but I don't know what a(n-1), a(n-2), etc. are in terms of n.

Solve recurrence: T(n) = T(n − 1) + T(n − 2) + 3

T(1) = T(2) = 1, and for n > 2, T(n) = T(n − 1) + T(n − 2) + 3.
What Ive done so far:
T(n-1) = T(n-2) + T(n-3) + 3 + 3
T(n-2) = T(n-3) + T(n-4) + 3 + 3 + 3
T(n) = T(n-2) + 2T(n-3) + T(n-4) + 3 + 3 + 3 + 3 + 3
T(n) = T(1) + 2T(2) + T(n-4) + 3(n + 2)
Im not sure if this is right, and if it is, how do I get rid of T(n-4).
These types of recurrences are tricky, and the repeated expansion method will unfortunately get you nowhere. Observing the recursion tree will only give you an upper bound, which is often not tight.
Two methods I can suggest:
1. Substitution + Standard Theorem
Make the following variable substitution:
This is in the correct form for the Akra-Bazzi method, with parameters:
2. Fibonacci formula
The Fibonacci series has an explicit formula which can be derived by guessing a solution of the form Fn = a^n. Using this as an analogy, substitute a similar expression for T(n):
Equating the constant and exponential terms:
Take the positive root because the negative root has absolute value less than 1, and will therefore decay to zero with increasing n:
Which is consistent with (1).

How to sort complex numbers by real part follow by imaginary part

I want to sort a list (or in julia speak an one dimensional array) of complex numbers, by real part then by imaginary part of the complex number. I tried using anonymous function for the lt but it does not work.
julia> b=[3 + 1im,1 + 2im,1 + 1im,5 + 6im]
4-element Array{Complex{Int64},1}:
3 + 1im
1 + 2im
1 + 1im
5 + 6im
julia> sort(b,lt = x,y -> if(real(x)==real(y)) imag(x)<imag(y) else real(x)<real(y) end)
ERROR: UndefVarError: x not defined
Stacktrace:
[1] top-level scope at none:0
I want the following results
1 + 1im
1 + 2im
3 + 1im
5 + 6im
So close!
julia> sort(b, lt = (x,y) -> real(x)==real(y) ? imag(x)<imag(y) : real(x)<real(y))
4-element Array{Complex{Int64},1}:
1+1im
1+2im
3+1im
5+6im
Tuples are sorted in lexicographic order (the 1st element; then the second one, and so on).
You could just do:
b = [3 + 1im, 1 + 2im, 1 + 1im, 5 + 6im]
sort(b, lt = (x, y) -> (real(x), imag(x)) < (real(y), imag(y)))
edit: The comment above, using by= , is much cleaner.

Solving the recurrence equation T(n) = 3 + m * T(n - m)

I have a Computer Science Midterm tomorrow and I need help determining the complexity of a particular recursive function as below, which is much complicated than the stuffs I've already worked on: it has two variables
T(n) = 3 + mT(n-m)
In simpler cases where m is a constant, the formula can be easily obtained by writing unpacking the relation; however, in this case, unpacking doesn't make the life easier as follows (let's say T(0) = c):
T(n) = 3 + mT(n-m)
T(n-1) = 3 + mT(n-m-1)
T(n-2) = 3 + mT(n-m-2)
...
Obviously, there's no straightforward elimination according to these inequalities. So, I'm wondering whether or not I should use another technique for such cases.
Don't worry about m - this is just a constant parameter. However you're unrolling your recursion incorrectly. Each step of unrolling involves three operations:
Taking value of T with argument value, which is m less
Multiplying it by m
Adding constant 3
So, it will look like this:
T(n) = m * T(n - m) + 3 = (Step 1)
= m * (m * T(n - 2*m) + 3) + 3 = (Step 2)
= m * (m * (m * T(n - 3*m) + 3) + 3) + 3 = ... (Step 3)
and so on. Unrolling T(n) up to step k will be given by following formula:
T(n) = m^k * T(n - k*m) + 3 * (1 + m + m^2 + m^3 + ... + m^(k-1))
Now you set n - k*m = 0 to use the initial condition T(0) and get:
k = n / m
Now you need to use a formula for the sum of geometric progression - and finally you'll get a closed formula for the T(n) (I'm leaving that final step to you).

How to calculate the explicit form of a recursive function?

I have this recursive function:
f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(1) = 2
f(2) = 8
I know from experience that explicit form of it would be:
f(n) = 3 ^ n - 1 // pow(3, n) - 1
I wanna know if there's any way to prove that. I googled a bit, yet didn't find anything simple to understand. I already know that generation functions probably solve it, they're too complex, I'd rather not get into them. I'm looking for a simpler way.
P.S.
If it helps I remember something like this solved it:
f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
// consider f(n) = x ^ n
x ^ n = 2 * x ^ (n-1) + 3 * x ^ (n-2) + 4
And then you somehow computed x that lead to explicit form of the recursive formula, yet I can't quite remember
f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(n+1) = 2 * f(n) + 3 * f(n-1) + 4
f(n+1)-f(n) = 2 * f(n) - 2 * f(n-1) + 3 * f(n-1) - 3 * f(n-2)
f(n+1) = 3 * f(n) + f(n-1) - 3 * f(n-2)
Now the 4 is gone.
As you said the next step is letting f(n) = x ^ n
x^(n+1) = 3 * x^n + x^(n-1) - 3 * x^(n-2)
divide by x^(n-2)
x^3 = 3 * x^2 + x - 3
x^3 - 3 * x^2 - x + 3 = 0
factorise to find x
(x-3)(x-1)(x+1) = 0
x = -1 or 1 or 3
f(n) = A * (-1)^n + B * 1^n + C * 3^n
f(n) = A * (-1)^n + B + C * 3^n
Now find A,B and C using the values you have
f(1) = 2; f(2) = 8; f(3) = 26
f(1) = 2 = -A + B + 3C
f(2) = 8 = A + B + 9C
f(3) = 26 = -A + B + 27C
solving for A,B and C:
f(3)-f(1) = 24 = 24C => C = 1
f(2)-f(1) = 6 = 2A + 6 => A = 0
2 = B + 3 => B = -1
Finally
f(n) = 3^n - 1
Ok, I know you didn't want generating functions (GF from now on) and all the complicated stuff, but my problem turned out to be nonlinear and simple linear methods didn't seem to work. So after a full day of searching, I found the answer and hopefully these findings will be of help to others.
My problem: a[n+1]= a[n]/(1+a[n]) (i.e. not linear (nor polynomial), but also not completely nonlinear - it is a rational difference equation)
if your recurrence is linear (or polynomial), wikihow has step-by-step instructions (with and without GF)
if you want to read something about GF, go to this wiki, but I didn't get it till I started doing examples (see next)
GF usage example on Fibonacci
if the previous example didn't make sense, download GF book and read the simplest GF example (section 1.1, ie a[n+1]= 2 a[n]+1, then 1.2, a[n+1]= 2 a[n]+1, then 1.3 - Fibonacci)
(while I'm on the book topic) templatetypedef mentioned Concrete Mathematics, download here, but I don't know much about it except it has a recurrence, sums, and GF chapter (among others) and a table of simple GFs on page 335
as I dove deeper for nonlinear stuff, I saw this page, using which I failed at z-transforms approach and didn't try linear algebra, but the link to rational difference eqn was the best (see next step)
so as per this page, rational functions are nice because you can transform them into polynomials and use linear methods of step 1. 3. and 4. above, which I wrote out by hand and probably made some mistake, because (see 8)
Mathematica (or even the free WolframAlpha) has a recurrence solver, which with RSolve[{a[n + 1] == a[n]/(1 + a[n]), a[1] == A}, a[n], n] got me a simple {{a[n] -> A/(1 - A + A n)}}. So I guess I'll go back and look for mistake in hand-calculations (they are good for understanding how the whole conversion process works).
Anyways, hope this helps.
In general, there is no algorithm for converting a recursive form into an iterative one. This problem is undecidable. As an example, consider this recursive function definition, which defines the Collatz sequence:
f(1) = 0
f(2n) = 1 + f(n)
f(2n + 1) = 1 + f(6n + 4)
It's not known whether or not this is even a well-defined function or not. Were an algorithm to exist that could convert this into a closed-form, we could decide whether or not it was well-defined.
However, for many common cases, it is possible to convert a recursive definition into an iterative one. The excellent textbook Concrete Mathematics spends much of its pages showing how to do this. One common technique that works quite well when you have a guess of what the answer is is to use induction. As an example for your case, suppose that you believe that your recursive definition does indeed give 3^n - 1. To prove this, try proving that it holds true for the base cases, then show that this knowledge lets you generalize the solution upward. You didn't put a base case in your post, but I'm assuming that
f(0) = 0
f(1) = 2
Given this, let's see whether your hunch is correct. For the specific inputs of 0 and 1, you can verify by inspection that the function does compute 3^n - 1. For the inductive step, let's assume that for all n' < n that f(n) = 3^n - 1. Then we have that
f(n) = 2f(n - 1) + 3f(n - 2) + 4
= 2 * (3^{n-1} - 1) + 3 * (3^{n-2} - 1) + 4
= 2 * 3^{n-1} - 2 + 3^{n-1} - 3 + 4
= 3 * 3^{n-1} - 5 + 4
= 3^n - 1
So we have just proven that this recursive function does indeed produce 3^n - 1.

Resources