/
Sym2.R
executable file
·465 lines (379 loc) · 12 KB
/
Sym2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# experimental user interface for Ryacas
# Not yet complete.
# To run, load Ryacas library and then source this code.
# Then try these examples:
# Example:
# x <- Sym("x")
# x*x+1
# Internals:
# - A Sym (i.e. symbol) object is a character string with class "Sym".
# - The ordinary R operators: +, -, etc. can operate on Sym objects
# giving new Sym objects.
# - printing a Sym object causes it to be passes to yacas and back
#' Sym
#'
#' The Symbol interface to yacas.
#'
#' An object of class \code{"Sym"} is internally a yacas character string. An
#' object of class \code{"Expr"} is internally an R expression. One can combine
#' such objects using the Math and Ops R operators (see help(Math) and
#' help(Ops) for a list). Also there are methods for a number of R generics:
#' \code{as.character.Sym}, \code{as.expression.Sym}, \code{determinant.Sym},
#' \code{deriv.Sym} and \code{print.Sym} and yacas-oriented functions: Clear,
#' Conjugate, Expand, Factor, Factorial, I, Identity, Infinity, Integrate,
#' Inverse, InverseTaylor, Limit, List, N, Newton, Pi, Precision, PrettyForm,
#' PrettyPrinter, Set, Simplify, Solve, Subst, Taylor, TeXForm, Transpose,
#' Ver and "%Where%" all of which have the same meaning as the corresponding yacas
#' commands. Try \code{vignette("Rycas-Sym")} for many examples.
#'
#' Get `Sym` objects with `getSyms()`.
#'
#' @aliases Sym Expr Exprq Ops.Expr Math.Expr deriv.Expr print.Expr
#' as.character.Expr as.Sym as.Sym.Expr
#' as.Sym.yacas
#' as.character.Sym
#' as.expression.Sym deriv.Sym Integrate OpenMath2R Ops.Sym Math.Sym
#' Ops.yacas.symbol print.Sym determinant.Sym print.yacas Sym SymExpr trans
#' transtab yacas.symbol.value yDeriv yFactorial yIntegrate yLimit yrewrite
#' yUnlist Simplify Factorial List Ver N Pi Clear Factor Expand Taylor
#' InverseTaylor PrettyForm TeXForm Precision Conjugate PrettyPrinter Solve
#' Newton Set Infinity I Limit Inverse Transpose as.Expr.formula Clear.Expr Clear.default
#' Conjugate.Expr Conjugate.default determinant.Expr Expand.Expr Expand.default
#' Factor.Expr Factor.default Factorial.Expr Factorial.default Integrate.Expr
#' Integrate.default Inverse.Expr Inverse.default InverseTaylor.default
#' Limit.Expr Limit.default List.Expr List.default N.Expr N.default Newton.Expr
#' Newton.default Precision.Expr Precision.default PrettyForm.Expr
#' PrettyForm.default PrettyPrinter.Expr PrettyPrinter.default Simplify.Expr
#' Simplify.default Solve.Expr Solve.default Taylor.Expr Taylor.default
#' TeXForm.Expr TeXForm.default Ver.Expr Ver.default Identity.default Identity
#' Subst Subst.default %Where% %Where%.default
#' CharacteristicEquation FindRoots EigenValues
#' @param x An R expression.
#' @param \dots An R character string or object that can be coerced to a
#' character string.
#' @usage
#' Sym(...)
#' Expr(x)
#' @seealso [as.Sym.matrix()]
#' @return \code{Sym} returns a \code{"Sym"} object and \code{Expr} returns an
#' \code{"Expr"} object.
#' @note Currently the only \code{Expr} methods implemented are
#' as.character.Expr, deriv.Expr, Math.Expr, Ops.Expr and print.Expr.
#' @keywords symbolmath
#' @examples
#'
#' x <- Sym("x")
#' x*x
#' Integrate(x*x, x)
#' Sym("%") %Where% list(x = 10)
#'
#' acos(Sym("1/2"))
#'
#' y <- Exprq(x)
#' y*y
#' deriv(y*y, y)
#' Exprq(acos(1/2))
#'
#' @export
Sym <- function(...) {
args <- list(...)
value <- if (length(args) > 1) paste("(", ..., ")") else paste(args[[1]])
class(value) <- c("Sym", "character")
value
}
#' @export
#as.character.Sym <- function(x, ...) as.character(unclass(x))
as.character.Sym <- function(x, ...) as.character(yacas(x))
#' @export
as.expression.Sym <- function(x, ...) yacas(x, ...)[[1]]
#' @export
as.Sym <- function(x, ...) UseMethod("as.Sym")
#' @export
as.Sym.yacas <- function(x, ...) Sym(format(yparse(x[[1]])))
#' @export
as.Sym.Expr <- function(x, ...) Sym(format(yparse(x)))
#' List `Sym()` objects
#'
#' Lists all `Sym()` objects in the global environment (`.GlobalEnv`)
#'
#' @param all.names a logical value. If `TRUE`, all object names are returned. If `FALSE`, names which begin with a `.` are omitted.
#'
#' @examples getSyms()
#' xs <- Sym("x")
#' getSyms()
#'
#' @importFrom methods is
#'
#' @export
getSyms <- function(all.names = FALSE) {
objs <- ls(envir = .GlobalEnv, all.names = all.names)
obj_isSym <- unlist(lapply(objs, function(nm) {
obj <- get(nm, .GlobalEnv)
return(is(obj, "Sym"))
}))
syms <- objs[obj_isSym]
return(syms)
}
#' Convert character vector to yacas object
#'
#' Simple and raw conversion to yacas
#'
#' @param x An R character vector.
#' @param \dots Not used
#'
#' @examples
#' x <- c("a", "2", "4", "c", "d", "6")
#' x
#' y <- as.Sym(x)
#' y
#' Eval(y, list(a = 3, c = 3, d = 3))
#'
#' @export
as.Sym.character <- function(x, ...) {
stopifnot(is.vector(x))
stopifnot(is.character(x))
z <- paste0("{ ", paste0(x, collapse = ", "), " }")
z3 <- Sym(z)
return(z3)
}
#' Convert character matrix to yacas object
#'
#' Simple and raw conversion to yacas
#'
#' @param x An R character matrix.
#' @param \dots Not used
#'
#' @examples
#' x <- matrix(c("a", "2", "4", "c", "d", "6"), 3, 2)
#' x
#' y <- as.Sym(x)
#' y
#' Eval(y, list(a = 3, c = 3, d = 3))
#'
#' @export
as.Sym.matrix <- function(x, ...) {
stopifnot(is.matrix(x))
stopifnot(is.character(x))
z <- apply(x, 1, function(z1) paste0("{ ", paste0(z1, collapse = ", "), " }"))
z2 <- paste0("{ ", paste0(z, collapse = ", "), " }")
z3 <- Sym(z2)
return(z3)
}
#' @export
Ops.Sym <- function (e1, e2)
if (missing(e2)) { Sym(.Generic, e1)
} else Sym(e1, .Generic, e2)
#' @export
Math.Sym <- function(x, ...) {
idx <- match(.Generic, transtab[,1], nomatch = 0)
fn <- if (idx > 0) transtab[idx, 3] else .Generic
Sym(fn, "(", x, ")")
}
#' @export
print.Sym <- function(x, ...) print(yacas(unclass(x), ...))
#' @export
deriv.Sym <- function(expr, name = "x", n = 1, ...)
Sym("D(", name, ",", n, ")", expr)
#' @export
Integrate <- function(f, ...) UseMethod("Integrate")
#' @export
Integrate.default <- function(f, x, a, b, ...) {
if (missing(a) && missing(b)) { Sym("Integrate(", x, ")", f)
} else Sym("Integrate(", x, ",", a, ",", b, ")", f)
}
#' @export
Eval.Sym <- function(x, env = parent.frame(), ...) {
# FIXME: Introduce S3 class "SymMat"/"SymVec" and exploit?
yacres <- yacas(unclass(x))
res <- eval(yacres[[1]], envir = env)
if (!is.null(yacres$LinAlgForm)) {
if (yacres$LinAlgType == "Vector") {
return(unlist(res))
} else if (yacres$LinAlgType == "Matrix") {
resmat <- matrix(unlist(res),
nrow = yacres$LinAlgDim[1],
ncol = yacres$LinAlgDim[2],
byrow = TRUE)
return(resmat)
}
}
return(res)
}
#' @export
Simplify <- function(x, ...) UseMethod("Simplify")
#' @export
Simplify.default <- function(x, ...) Sym("Simplify(", x, ")")
#' @export
Transpose <- function(x, ...) UseMethod("Transpose")
#' @export
Transpose.default <- function(x, ...) Sym("Transpose(", x, ")")
#' @export
Factorial <- function(x) UseMethod("Factorial")
#' @export
Factorial.default <- function(x) Sym("Factorial(", x, ")")
#' @export
List <- function(x, ...) UseMethod("List")
#' @export
List.default <- function(x, ...) Sym("List(", paste(x, ..., sep = ","), ")")
#' @export
N <- function(x, ...) UseMethod("N")
#' @export
N.default <- function(x, ...) Sym("N(", paste(x, ..., sep = ","), ")")
#' @export
Pi <- Sym("Pi")
#' @export
Ver <- function(x) UseMethod("Ver")
#' @export
Ver.default <- function(x) Sym("Version()")
#' @export
Clear <- function(x, ...) UseMethod("Clear")
#' @export
Clear.default <- function(x, ...) Sym("Clear(", x, ")")
#' @export
Factor <- function(x) UseMethod("Factor")
#' @export
Factor.default <- function(x) Sym("Factor(", x, ")")
#' @export
Expand <- function(x, ...) UseMethod("Expand")
#' @export
Expand.default <- function(x, ...) Sym("Expand(", x, ")")
#' @export
Taylor <- function(f, ...) UseMethod("Taylor")
#' @export
Taylor.default <- function(f, x, a, n, ...)
Sym("Taylor(", x, ",", a, ",", n, ")", f)
#' @export
InverseTaylor <- function(x, ...) UseMethod("Taylor")
#' @export
InverseTaylor.default <- function(f, x, a, n, ...)
Sym("InverseTaylor(", x, ",", a, ",", n, ")", f)
#' @export
PrettyForm <- function(x, ...) UseMethod("PrettyForm")
#' @export
PrettyForm.default <- function(x, ...) Sym("PrettyForm(", x, ")")
#' @export
TeXForm <- function(x, ...) UseMethod("TeXForm")
# @export
#TeXForm.default <- function(x, ...) Sym("TeXForm(", x, ")")
#' @export
TeXForm.default <- function(x, ...) as.character(as.expression(Sym("TeXForm(", x, ")")))
#' @export
Precision <- function(x, ...) UseMethod("Precision")
#' @export
Precision.default <- function(x, ...) Sym("Precision(", x, ")")
#' @export
Conjugate <- function(x, ...) UseMethod("Conjugate")
#' @export
Conjugate.default <- function(x, ...) Sym("Conjugate(", x, ")")
#' @export
PrettyPrinter <- function(x, ...) UseMethod("PrettyPrinter")
#' @export
PrettyPrinter.default <- function(x, ...) {
if (missing(x)) Sym("PrettyPrinter()")
else Sym(paste('PrettyPrinter("', x, '")', sep = ""))
}
#' @export
Solve <- function(x, ...) UseMethod("Solve")
#' @export
Solve.default <- function(x, y, ...) Sym("Solve(", x, ",", y, ")")
#' @export
Newton <- function(x, ...) UseMethod("Newton")
#' @export
Newton.default <- function(x, ...) Sym("Newton(", paste(x, ..., sep = ","), ")")
#' @export
Set <- function(x, value) {
if (inherits(value, "expression"))
yacas(substitute(Set(x, value, as.list(match.call())[-1])))
else
yacas(unclass(Sym(deparse(substitute(x)), ":=", value)))
}
#' @export
Infinity <- Sym("Infinity")
#' @export
I <- Sym("I")
#' @export
Limit <- function(f, ...) UseMethod("Limit")
#' @export
Limit.default <- function(f, x, a, ...) Sym("Limit(", x, ",", a, ")", f)
#' @export
Subst <- function(expr, ...) UseMethod("Subst")
#' @export
Subst.default <- function(expr, x, replacement, ...)
Sym("Subst(", x, ",", replacement, ")", expr)
#' @export
Inverse <- function(x, ...) UseMethod("Inverse")
#' @export
Inverse.default <- function(x, ...) Sym("Inverse(", x, ")")
#' @export
determinant.Sym <- function(x, ...) Sym("Determinant(", x, ")")
#' @export
Identity <- function(x) UseMethod("Identity")
#' @export
Identity.default <- function(x) Sym("Identity(", x, ")")
#' @export
"%Where%" <- function(x, y) UseMethod("%Where%")
#' @export
"%Where%.default" <- function(x, y) {
Sym(x, "Where", paste("{", names(y)[[1]], "==", Sym(y[[1]]), "}"))
}
#'
#
# #' @export
# CharacteristicEquation <- function(mat, freevar, ...) UseMethod("CharacteristicEquation")
#
# #' @export
# CharacteristicEquation.Sym <- function(mat, freevar, ...) {
# stopifnot(is.character(mat))
# stopifnot(is.character(freevar))
#
# return(yacas(paste0("CharacteristicEquation(", as.character(mat), ", ", freevar, ")")))
#
# #var_chr <- substitute(var)
# #return(yacas(paste0("CharacteristicEquation(", as.character(mat), ", ", var_chr, ")")))
# }
#
#
#
# #' @export
# FindRoots <- function(expr, var, ...) UseMethod("FindRoots")
#
# #' @export
# FindRoots.default <- function(expr, var, ...) {
# stopifnot(is.character(var))
# return(yacas(paste0("Solve(0 == ", expr, ", ", var, ")")))
# }
#
# #' @export
# EigenValues <- function(mat, ...) UseMethod("EigenValues")
#
# #' @export
# EigenValues.Sym <- function(mat, ...) {
# stopifnot(is.character(mat))
# return(yacas(paste0("EigenValues(", mat, ")")))
# }
#
# #' @export
# EigenValues <- function(mat, ...) UseMethod("EigenValues")
#
# #' @export
# EigenValues.default <- function(mat, ...) {
# stopifnot(is.character(mat))
# stopifnot(is(mat, "Sym"))
# dots <- list(...)
#
# var_chr_org <- "lambda"
# var_chr <- var_chr_org
# var_chr_i <- 1
# matform <- yacas(mat)$LinAlgForm
# symbol_candidates <- unique(c(matform))
# while (var_chr %in% symbol_candidates) {
# print(var_chr)
# var_chr <- paste0(var_chr_org, var_chr_i)
# var_chr_i <- var_chr_i + 1
# }
#
# chr_eq <- CharacteristicEquation(mat = mat, freevar = var_chr)
# chr_eq_roots <- FindRoots(expr = chr_eq, var = var_chr)
#
# return(chr_eq_roots)
# }