-1

I want to write a function to calculate the determinant of a squared matrix be a 2x2 or 3x3 in just one function such that if its users supply just 4 elements it will take it to be a 2x2 and if its users supply 9 elements it will use the 3x3 function. Here are my functions for the 2x2 and the 3x3:

matrix(1:4, ncol=2)
#     [,1] [,2]
#[1,]    1    3
#[2,]    2    4
A_22 <- function(x11, x12, x21, x22) {
  (x11 * x22) - (x12 * x21)
}
A_22(x11 = 1, x12 = 2, x21 =3, x22 = 4)
#[1] -2
cbind(1,1:3,c(2,0,1))
#     [,1] [,2] [,3]
#[1,]    1    1    2
#[2,]    1    2    0
#[3,]    1    3    1

A_33 <- function(x11, x12, x13, x21, x22, x23, x31, x32, x33) {
  (x11 * x22 * x33) + (x12 * x23 * x31) + (x13 * x21 * x33) - (x31 * x22 * x13) - (x32 * x23 * x11) - (x33 * x21 * x12)
}
A_33(x11 = 1, x12 = 1, x13 = 2, x21 = 1, x22 = 2, x23 = 0, x31 = 1, x32 = 3, x33 = 1)
#[1] -1

I viewed this solution but it is only applicable to using different methods to solve a problem and not like this question of handling different cases.

What is the elegant way of combining the two different cases of A_22 and A_33 to a parent function?

Daniel James
  • 1,381
  • 1
  • 10
  • 28
  • https://stat.ethz.ch/R-manual/R-devel/library/base/html/det.html — note that this yields a different result than your `A_33` function. – Konrad Rudolph Oct 13 '21 at 11:21
  • @KonradRudolph Thank you for your suggestion but I am trying to learn and not looking for a ready-made solution. I created the problem using the determinant just as a case study to learn and be able to apply the method to other circumstances. – Daniel James Oct 13 '21 at 11:27
  • 1
    In that case you’ll need to turn to the definition of the determinant and create a general function based on an understanding of the general algorithm. — You can’t just merge two arbitrary functions into one: there’s no obvious relationship between them. Unfortunately writing a general matrix determinant function isn’t that trivial; you could apply the rule of Sarrus but this requires you to find all matrix diagonals, and R has no built-in way for this as far as I’m aware (deriving them isn’t hard, but it *is* fiddly). – Konrad Rudolph Oct 13 '21 at 11:43
  • [diagonals](https://stackoverflow.com/questions/27935555/get-all-diagonal-vectors-from-matrix/27935808) in combination with `lengths`, using @20650 approach with `split` gets forward and reverse diagonals. Trying to apply to `Gbemi` expansion for nxn, n=5, fitfully. – Chris Oct 14 '21 at 12:44
  • @DanielJames please check if my solution fits your needs... I used recursion to solve the problem and calculate determinants. It was fun figuring out how to do it... – Diego Oct 16 '21 at 11:16
  • It does not fit. – Daniel James Oct 16 '21 at 11:17

1 Answers1

1

Please check if this solves your problem:

Det_fn <- function(x){
if(nrow(x)!=ncol(x)) {
  print("Undefined")
} else
  if(nrow(x)==1 && ncol(x)==1) {
    DET <- x[1,1]; DET
  } else
    if(nrow(x)==2 && ncol(x)==2) {
      DET <- x[1,1]*x[2,2]-x[1,2]*x[2,1]; DET
    } else
      for(j in 1:ncol(x)){
        DET[j] <- (((-1)^(1+j))*x[1,j]*Det_fn(x[-1,-j]))
      }
  DET
  sum(DET)
}

If I understood correctly you want to call the same function inside the function in some sort of recursion: The example of the determinat of a matrix calculation is most pertinent as it involves nested for loops and calling the determinant function inside the very same function. I have updated the function to correct some mistakes and now I can calculate this for example:

x <- matrix(1:16, ncol=4); x

In the function if the number of columns and rows is different, the function returns a message indicating that the determinant is undefined. If there´s a single element in the matrix, the determinant is value of that isngle element. The determinant of the 2X2 case is next shown. When the matrix is larger than 2X2, it calculates the determinant by using recursion... Try it please and let me know if this solves the problem.

Diego
  • 328
  • 2
  • 9