Find the eigenvalues of a generic matrix with R [closed] - r

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
does anyone of you knows a way to use R so to calculate and display the characteristic polynomial of the eigenvalues of a generic matrix composed by chr elements?
say i.e.
m <- matrix(c('a','b','c','d','e','f','g','h','i','l','m','n'),4,4)
Please consider that I have to apply this method to a very large matrix
Thank you in advance

You can do this with the Ryacas package, but you'll have to jump through the necessary hoops to install Yacas on your system first.
library("Ryacas")
m <- matrix(letters[1:16],4,byrow=TRUE)
yrow <- function(x) paste0("{",paste(x,collapse=","),"}")
yrow(m[1,]) ## "{a,b,c,d}"
ymat <- function(x) yrow(apply(x,1,yrow))
cheqstr <- function(x) {
paste0("Expand(CharacteristicEquation(",
ymat(x),",x),x)")
}
yacas(cheqstr(m))
## (a-x)*(f-x)*(k-x)*(p-x)-(a-x)*(f-x)*l*o+(a-x)*h*j*o-d*e*j*o-
## (a-x)*g*j*(p-x)+(a-x)*g*l*n-(a-x)*h*(k-x)*n+d*e*(k-x)*n+c*e*j*(p-x)-
## c*e*l*n+c*h*i*n-d*g*i*n-b*e*(k-x)*(p-x)+b*e*l*o-b*h*i*o+d*(f-x)*i*o+
## b*g*i*(p-x)-b*g*l*m+b*h*(k-x)*m-d*(f-x)*(k-x)*m-c*(f-x)*i*(p-x)+
## c*(f-x)*l*m-c*h*j*m+d*g*j*m;

Related

Is there an R function to solve "find two numbers whose sum is x and product is a maximum" like problems? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to know if there is library like lpSolve for solving that kind of problems, I do not know if this library allows to include in the constraints the product of the two variables or how I can specify "the product is a maximum".
Defining a projection onto the feasible region allows us to use unconstrained optimization to optimize this:
tot <- 10
proj <- function(x) tot * x / sum(x)
res <- optim(1:2, function(x) -prod(proj(x)))
res$convergence
## [1] 0
-res$value
## [1] 25
proj(res$par)
## [1] 4.999799 5.000201

Using Apply() Function to a matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Given a table I need to use apply() to find t the correlation between each one of the 8 variables in the state.x77 matrix and the Population variable. state.x77 is a built in matrix with 8 columns.
I had to first create a function called cor_var due to the instructions and then have to use apply(). So here is my input:
cor_var=function(v1,v2=state.x77[,"Income"]){cor(v1,v2)}
apply(mat,2,cor_var,v2=state.x77[,"Population"])
the v2 is the extra optional argument for apply() ... argument, so this should work but it is returning Error in cor(v1, v2) : incompatible dimensions. Any help on where I am wrong would be appreciated. I have to use cor_var and apply two functions btw, can't use lappy or mapply.
You can use :
apply(state.x77,2,function(x) cor(x, state.x77[,"Income"]))
#Population Income Illiteracy Life Exp Murder HS Grad Frost Area
# 0.2082276 1.0000000 -0.4370752 0.3402553 -0.2300776 0.6199323 0.2262822 0.3633154
We can use
apply(mat,2,cor_var,v2=state.x77[,"Population"])

Function for matrix in R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm new (very new) in R. I'm struggling with making a function that's supposed to take a matrix (old_matrix) and return a new matrix (new_matrix), but in new_matrix all values in old_matrix that is a prime should be multiplied by 2 when it appears in new_matrix. So the new matrix should look the same as the old matrix, but where a prime occurs in old, this element should be multiplied by 2.
I'm thinking that I should start out with a for loop, but I'm already struggling with how to make the loop go through all elements of the matrix. I appreciate all the help I can get to get closer to making this function!
The isPrime function in the numbers package could be a big help
# Start by creating an example to work with
old_matrix <- matrix(sample.int(100, 25), 5, 5)
# Create your new matrix and determine which numbers are prime
new_matrix <- old_matrix
primeVals <- numbers::isPrime(old_matrix)
# Index into the matrix using the prime value indicator and multiply by 2
new_matrix[primeVals] <- new_matrix[primeVals]*2

How R multiples vectors that are not compatible for multiplication [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When I submit this code to R:
x <- c(1,2,4)
z <- c(7,6,3)
a <- x * z
I get:
a
[1] 7 12 12
So R just multiples element by element. But the two vectors are not compatible for multiplication because the first one has three columns and the second one does not have three rows.
What is happening internally?
Please note that these are vectors; not tables.
This means they can of course be multiplied with each other and would give the expected result through their inner product.

calculate peak values in a plot using R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table with two variables.The data is from NMR.So when I plot I get a spectrum.I found the peaks in plot.But I need to know how to list the values of peak and store them into a variable.Anyone please help.
An easy implementation based on Brian Ripley's post at R-help:
peaks <- function(x, halfWindowSize) {
windowSize <- halfWindowSize * 2 + 1
windows <- embed(x, windowSize)
localMaxima <- max.col(windows, "first") == halfWindowSize + 1
return(c(rep(FALSE, halfWindowSize), localMaxima, rep(FALSE, halfWindowSize)))
}
Example:
x <- c(1,3,1,3,1)
peaks(x, 1)
## [1] FALSE TRUE FALSE TRUE FALSE

Resources