[R] Substituting inside expression
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Feb 25 11:55:00 CET 2011
On Fri, Feb 25, 2011 at 4:11 AM, zbynek.janoska at gmail.com
<zbynek.janoska at centrum.cz> wrote:
>
> I am having following problem:
> I´m constructing model for calculation of area of triangle.
> I know sides a, b, and gamma angle.
> I wish to calculate the area using heron´s formula:
> S <- sqrt(s*(s-a)*(s-b)*(s-c))
> where
> s <- (a+b+c)/2
> and c is calculated using law of cosines:
> c <- sqrt(a^2 + b^2 -2*a*b*cos(gamma))
>
> since i am calculating a regression model, i need derivation of this
> expression for area S.
> something like (D(expression.S,c("a","b")))
>
> To write it all into a single expression, it is too complicated, so i would
> like to use some kind of substitution. however, if i try:
>
> s.e <- substitute(expression((a+b+c)/2), list(c =
> expression(sqrt(a^2+b^2-2*a*b*cos(gamma))))),
> I get
>>s.e
> expression((a + b + expression(sqrt(a^2 + b^2 - 2 * a * b * cos(gamma))))/2)
>
> which is not what I wanted
>
> Can someone point me to the right direction?
Try this:
> e <- substitute((a+b+c)/2, list(c = quote(sqrt(a^2+b^2-2*a*b*cos(gamma)))))
> D(e, "a")
(1 + 0.5 * ((2 * a - 2 * b * cos(gamma)) * (a^2 + b^2 - 2 * a * b *
cos(gamma))^-0.5))/2
Also
> library(Ryacas) # http://ryacas.googlecode.com
>
> a <- Sym("a"); b <- Sym("b"); gamma <- Sym("gamma")
>
> c <- sqrt(a^2+b^2-2*a*b*cos(gamma))
> deriv((a+b+c)/2, a)
expression(2 * ((2 * a - 2 * b * cos(gamma))/(2 * root(a^2 +
b^2 - 2 * a * b * cos(gamma), 2)) + 1)/4)
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list