Evaluate a symbolic Ryacas expression
Date : March 29 2020, 07:55 AM
will be helpful for those in need G.Grothendieck pointed out in comments that you'll need to first to capture the expression to be operated upon below: soln <- Solve(n/2*(2-exp(-lambda12*Tf)-exp(-lambda18*Tf))==d , n)
X <- yacas(soln)$text
X <- expression(list(n == 382/1.625))
res <- eval(X[[1]][[2]][[3]])
res
[1] 235.0769
as.list(X)
# [[1]]
# list(n == 382/1.625)
as.list(X[[1]])
# [[1]]
# list
#
# [[2]]
# n == 382/1.625
as.list(X[[1]][[2]])
# [[1]]
# `==`
#
# [[2]]
# n
#
# [[3]]
# 382/1.625
|
matrix chain mutiplication dynamic programming
Date : March 29 2020, 07:55 AM
around this issue First of all, it's pseudocode, and these arrays are 1-based. If you are using a C-like language, that will probably be the first issue, since arrays in C start at index 0 and end at len-1, if len is the length of array. Next, the variable n is chosen to be smaller than the total number of matrices by 1. If you replace n with p.length - 1, then it may also become a bit clearer what's going on. L = 4;
for (i = 1; i <= p.length - 4; i++)
{
...
}
L = 4;
for (i = 1; i <= p.length - 4; i++)
{
j = i + 3;
...
}
L = 4;
for (i = 1; i <= p.length - 4; i++)
{
j = i + 3;
m[i,j] = MAXINT;
for (k = i; k <= j - 1; k++)
{
// get the cost of splitting at `k`,
// i.e. chains (i, k) and (k + 1, j)
}
}
|
symbolic matrix multiplication by Ryacas
Date : March 29 2020, 07:55 AM
may help you . I define two matrices mat1 and mat2 in Sym class which is suitable for Ryacas symbolic computation: , Try this: > mat1 * mat2
expression(list(list(x^2 + 6, x^2 + 12 * x), list(x^4 + 3 * x,
x^4 + 6 * x^2)))
|
Element-wise mutiplication .* of vectors gives matrix in Matlab
Tag : matlab , By : user184406
Date : March 29 2020, 07:55 AM
will help you Due to implicit expansion ( introduced in 2016b) it's essentially the same as using bsxfun. But what does that mean? a = 1:3;
b = 2:4;
c = a.*b;
% c = [2 6 12], element-wise multiplication c(j) = a(j)*b(j)
c = b'*a;
% c = [2 4 5; 3 6 9; 4 8 12]
% standard matrix multiplication of vectors
% c(i,j) = a(i) + b(j)
c = bsxfun(@times, b', a)
% c = [2 4 5; 3 6 9; 4 8 12]
% bsxfun applies the function (in this case @times) to b' and a
% b' in singleton in the 2nd dimension, a is singleton in the 1st dimension
% Use repmat to perform the expansion to the correct size
repmat(b', 1, size(a,2)) .* repmat(a, size(b',1), 1)
% Equivalent to...
repmat(b', 1, 3) .* repmat(a, 3, 1)
% Equivalent to...
[2 2 2; 3 3 3; 4 4 4] .* [1 2 3; 1 2 3; 1 2 3]
% = [2 4 5; 3 6 9; 4 8 12] the same as b'*a
c = a.*b'; % Error: Matrix dimensions must agree.
c = b'.*a; % Error: Matrix dimensions must agree.
c = a.*b'; % [2 4 5; 3 6 9; 4 8 12] the same as bsxfun(@times, a, b')
c = b'.*a; % [2 4 5; 3 6 9; 4 8 12] the same as bsxfun(@times, b', a)
% These two are equivalent also because order of operations is irrelevant
% We can see this by thinking about the expansion discussed above
c = a(:).*b(:); % c = [2; 6; 12] always a column vector
|
Symbolic computation in R with Ryacas - results become character
Date : March 29 2020, 07:55 AM
may help you . I have a small MATLAB script mainly doing derivatives using symbolic toolbox that I want to rewrite into R. I chose Ryacas package because I found rSymPy too tricky to install... Here is my R code , There are several problems with the R code in the question: typeof(NA)
## [1] "logical"
f <- matrix(list(), 4, 4)
f[[1, 1]] <- z^2 * exp(-z) / (1 - exp(-z))
psi <- list()
psi[[1]] <- z^2 * exp(-z) / (1 - exp(-z))
Eval(f[[i, 1]], list(z = 1))
## [1] 0.2432798
z <- 1
Eval(f[[i, 1]])
vignette("Ryacas")
demo(package = "Ryacas")
# install.packages('Ryacas')
library(Ryacas)
z <- Sym("z")
psi <- list()
psi[[1]] <- z^2 * exp(-z) / (1 - exp(-z))
psi[[2]] <- z^2 * exp(-z) / (1 - exp(-z)) * log(z)
psi[[3]] <- z^2 * exp(-z) / (1 - exp(-z)) * log(z)^2
f <- matrix(list(), 4, 4)
f[[1,1]] <- z^2 * exp(-z) / (1 - exp(-z))
for(i in 2:4) {
f[[i, 1]] <- deriv(psi[[i-1]], z)
j <- 2
while(j <= i) {
f[[i, j]] <- deriv(f[[i, j-1]] / f[[j-1, j-1]], z)
j <- j + 1
}
}
i <- 2
deriv(psi[[i-1]], z)
f[[i, 1]]
Eval(f[[i, 1]], list(z = 1))
|