solve: Solve a system of equation

Description Usage Arguments Details Value Author(s) See Also Examples

Description

This generic function solves the equation a \%*\% x = b for x, where b can be either a vector or a matrix.

If a and b are rational, return is a rational matrix.

If a and b are big integers (of class bigz) solution is in Z/nZ if there is a common modulus, or a rational matrix if not.

Usage

1
2
3
4
## S3 method for class 'bigz'
solve(a, b, ...)
## S3 method for class 'bigq'
solve(a, b, ...)

Arguments

a,b

A element of class bigz or bigq

...

Unused

Details

It uses the Gauss and trucmuch algo ... (to be detailled).

Value

If a and b are rational, return is a rational matrix.

If a and b are big integers (of class bigz) solution is in Z/nZ if there is a common modulus, of a rational matrix if not.

Author(s)

Antoine Lucas

See Also

solve

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
x <- matrix(1:4,2,2)  ## standard solve :
solve(x)

q <- as.bigq(x) ## solve with rational
solve(q)

z <- as.bigz(x)
modulus(z) <- 7  ## solve in Z/7Z :
solve(z)

b <- c(1,3)
solve(q,b)
solve(z,b)

## Inversion of ("non-trivial") rational matrices :

A <- rbind(c(10, 1,  3),
           c( 4, 2, 10),
           c( 1, 8,  2))
(IA.q <- solve(as.bigq(A))) # fractions..
stopifnot(diag(3) == A %*% IA.q)# perfect

set.seed(5); B <- matrix(round(9*runif(5^2, -1,1)), 5)
B
(IB.q <- solve(as.bigq(B)))
stopifnot(diag(5) == B %*% IB.q, diag(5) == IB.q %*% B,
          identical(B, asNumeric(solve(IB.q))))

Example output

Attaching package: 'gmp'

The following objects are masked from 'package:base':

    %*%, apply, crossprod, matrix, tcrossprod

     [,1] [,2]
[1,]   -2  1.5
[2,]    1 -0.5
Big Rational ('bigq') 2 x 2 matrix:
     [,1] [,2]
[1,] -2   3/2 
[2,] 1    -1/2
     [,1]     [,2]    
[1,] (5 %% 7) (5 %% 7)
[2,] (1 %% 7) (3 %% 7)
Big Rational ('bigq') 2 x 1 matrix:
     [,1]
[1,] 5/2 
[2,] -1/2
     [,1]    
[1,] (6 %% 7)
[2,] (3 %% 7)
Big Rational ('bigq') 3 x 3 matrix:
     [,1]    [,2]    [,3]  
[1,] 19/167  -11/334 -1/167
[2,] -1/334  -17/668 22/167
[3,] -15/334 79/668  -4/167
     [,1] [,2] [,3] [,4] [,5]
[1,]   -5    4   -4   -5    7
[2,]    3    1    0   -2    4
[3,]    8    6   -3    7   -5
[4,]   -4    8    1    1   -5
[5,]   -7   -7   -4    6   -6
Big Rational ('bigq') 5 x 5 matrix:
     [,1]      [,2]      [,3]      [,4]      [,5]     
[1,] 35/347    -515/694  79/694    -183/694  -175/694 
[2,] -11/347   261/694   -5/694    117/694   55/694   
[3,] -124/347  1081/694  -151/694  341/694   273/694  
[4,] -469/1041 5513/2082 -217/1041 775/1041  1651/2082
[5,] -305/1041 2120/1041 -391/2082 1099/2082 589/1041 

gmp documentation built on May 29, 2017, 5:44 p.m.

Related to solve in gmp...