tex
e <- Ryacas::ysym("((2*Sin(2+x))^2 + (a+b)^2)^3")
e
#> y: ((2*Sin(x+2))^2+(a+b)^2)^3
o <- Ryacas::tex(e)
o
#> [1] "\\left( \\left( 2 \\sin \\left( x + 2\\right) \\right) ^{2} + \\left( a + b\\right) ^{2}\\right) ^{3}"
\[ \left( \left( 2 \sin \left( x + 2\right) \right) ^{2} + \left( a + b\right) ^{2}\right) ^{3} \]
All as hard brackets:
o2 <- hbtex(e)
o2
#> [1] "\\left[ \\left[ 2 \\sin \\left[ x + 2\\right] \\right] ^{2} + \\left[ a + b\\right] ^{2}\\right] ^{3}"
\[ \left[ \left[ 2 \sin \left[ x + 2\right] \right] ^{2} + \left[ a + b\right] ^{2}\right] ^{3} \]
Customised bracketing:
o3 <- btex(e)
o3
#> [1] " \\left\\{ \\left[ 2 \\sin \\left( x + 2 \\right) \\right] ^{2} + \\left[ a + b \\right] ^{2} \\right\\} ^{3}"
\[ \left\{ \left[ 2 \sin \left( x + 2 \right) \right] ^{2} + \left[ a + b \right] ^{2} \right\} ^{3} \]
An argument specifying brackets
can be given (recycled):
o4 <- btex(e,
brackets = list(
c("\\langle", "\\rangle"),
c("\\{", "\\}")))
o4
#> [1] " \\left\\langle \\left\\{ 2 \\sin \\left\\langle x + 2 \\right\\rangle \\right\\} ^{2} + \\left\\{ a + b \\right\\} ^{2} \\right\\rangle ^{3}"
\[ \left\langle \left\{ 2 \sin \left\langle x + 2 \right\rangle \right\} ^{2} + \left\{ a + b \right\} ^{2} \right\rangle ^{3} \]
2x
to mean 2*x
make_products_explicit("2x")
#> [1] "2*x"
make_products_explicit("2(1+2)")
#> [1] "2*(1+2)"
make_products_explicit("x(y)")
#> [1] "x*(y)"
make_products_explicit("(1+2)3") # Not allowed by definition
#> [1] "(1+2)3"
make_products_explicit("(1+2)x") # Is allowed
#> [1] "(1+2)*x"
make_products_explicit("x(2+1) + sin(y)")
#> [1] "x*(2+1)+sin(y)"
make_products_explicit("sin(2+1) + x(y+1)")
#> [1] "sin(2+1)+x*(y+1)"
make_products_explicit("(2x)(y+1)")
#> [1] "(2*x)*(y+1)"
By default, the allowed functions are those of getGroupMembers("Math")
:
getGroupMembers("Math")
#> [1] "abs" "sign" "sqrt" "ceiling" "floor" "trunc"
#> [7] "cummax" "cummin" "cumprod" "cumsum" "exp" "expm1"
#> [13] "log" "log10" "log2" "log1p" "cos" "cosh"
#> [19] "sin" "sinh" "tan" "tanh" "acos" "acosh"
#> [25] "asin" "asinh" "atan" "atanh" "cospi" "sinpi"
#> [31] "tanpi" "gamma" "lgamma" "digamma" "trigamma"
This can be changed:
By default, safe evaluation inserts inner products.
safe_eval("2.2", vars = NULL)
#> [1] 2.2
safe_eval("2.2x^2", vars = list(x = 0.2))
#> [1] 0.088
safe_eval("2.2*x^2", vars = list(x = 0.2))
#> [1] 0.088
safe_eval("2,2*x^2", vars = list(x = 0.2))
#> [1] 0.088
Efterything after first ;
is ignored:
grd <- expand.grid(x = seq(-10, 10, length.out = 10))
answer <- Ryacas::ysym("2*x^2")
compare_reply_answer(reply = "2x^2",
answer = answer,
compare_grid = grd)
#> [1] TRUE
compare_reply_answer(reply = "2.3x^2",
answer = answer,
compare_grid = grd)
#> [1] FALSE
m <- matrix(c("2+z^3", "2*sin(x)", "3*y^2", "1.1*x^2"), 2, 2)
m
#> [,1] [,2]
#> [1,] "2+z^3" "3*y^2"
#> [2,] "2*sin(x)" "1.1*x^2"
ans <- Ryacas::ysym(m)
ans
#> {{ z^3+2, 3*y^2},
#> {2*sin(x), 1.1*x^2}}
grd <- expand.grid(x = seq(-10, 10, length.out = 5),
y = seq(-10, 10, length.out = 5),
z = seq(-10, 10, length.out = 5))
compare_reply_answer(reply = m,
answer = ans,
compare_grid = grd)
#> [1] TRUE
compare_reply_answer(reply = m[1, 1],
answer = ans,
compare_grid = grd)
#> [1] FALSE
m2 <- apply(m, 2, paste0, '+0.1')
m2
#> [,1] [,2]
#> [1,] "2+z^3+0.1" "3*y^2+0.1"
#> [2,] "2*sin(x)+0.1" "1.1*x^2+0.1"
compare_reply_answer(reply = m2,
answer = ans,
compare_grid = grd)
#> [1] FALSE