3

I looked at several posts about this error. However, I am finding that my matrices are the appropriate sizes and I'm still getting this error. I next thought the issue was class, but they are matrix objects. I'm not sure what's going on. Here is the function I'm writing:

library(Matrix)
library(MASS)

modify <- function(Vandermonde) {
    s = svd(Vandermonde)
    k = which(s$d < 1e-1)
    u = matrix(s$u[,-k], nrow = nrow(s$u), byrow = FALSE)
    v = matrix(s$v[,-k], nrow = nrow(s$v), byrow = FALSE)

    modify = u * diag(s$d[-k]) * t(v)
}

Basically, I'm writing a function that takes a rectangular matrix, checks to see if it's singular. If it is, make it non-singular. The matrix I am checking is a Vandermonde, which I create outside this function. It is rectangular because I have N rows and m powers. These are specified by whomever. I need the Vandermonde to solve the problem

V(n)*x = f(n)

where V is made of n = {1, 2, 3, 4, ..., N} and f(n) are corresponding terms of an integer sequence. An example sequence is H =

Place        Value
  1 1.000000e+00
  2 3.000000e+00
  3 1.300000e+01
  4 8.700000e+01
  5 1.053000e+03
  6 2.857600e+04
  7 2.141733e+06
  8 5.081471e+08
  9 4.021353e+11
 10 1.073376e+15
 11 9.700385e+18
 12 2.984343e+23
 13 3.147936e+28
 14 1.147438e+34

And I create the Vandermonde with

mat = matrix(0,n, m + 1)
for (i in 1:n ) {
 for (j in 1:(m + 1)) {
  mat[i,j] = input[i] ^ (j - 1) 
 }
}

where in the case of H the n = 14 and I let m = 10. To note, input is H$Place and the expected output is H$Value.

user1723196
  • 125
  • 1
  • 10
  • 1
    Can you please include data and/or code that will provide us with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? – Ben Bolker Jun 08 '16 at 01:02
  • @BenBolker: Thank you for your interest. I've modified my question to include data and code to help you see what I'm seeing. – user1723196 Jun 08 '16 at 01:31
  • `dim(diag(s$d[-k]))` is 9x9; `dim(t(v))` is 9x11. Do you mean to matrix-multiply (`%*%`) them instead of taking the Hadamard (elementwise) product? (Are you coming from MATLAB where `*` denotes matrix multiplication?) – Ben Bolker Jun 08 '16 at 01:39
  • @BenBolker: I am. Thanks so much. I tried finding the proper notation online, but I found only * or %% ... it wasn't clear that I needed both. – user1723196 Jun 08 '16 at 01:43
  • does that solve the problem? (I didn't take the time to really understand your problem, so I don't know if getting past that issue solves the whole problem ...) – Ben Bolker Jun 08 '16 at 01:50
  • It did solve the problem. Thank you. – user1723196 Jun 08 '16 at 01:59

1 Answers1

2

Based on our conversation in the comments, you need to use

modify = u %*% diag(s$d[-k]) %*% t(v)

in your function in order to perform matrix multiplication. In R (in contrast to MATLAB), * denotes the Hadamard product (elementwise multiplication, .* in MATLAB), which can only be done on matrices with identical dimensions, while %*% denotes ordinary matrix multiplication, which requires only ncol(A)==nrow(B) for A %*% B.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453