File: mathml.R

package info (click to toggle)
r-cran-xml 3.99-0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 6,659; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (232 lines) | stat: -rw-r--r-- 4,560 bytes parent folder | download | duplicates (11)
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
#
# Functions to illustrate how to convert a MathML tree an 
# R expression.
#
#

mathml <-
# generic method that converts an XMLNode
# object to an R/S expression.
function(node)
{
  UseMethod("mathml", node)
}



mathml.XMLDocument <-
function(doc)
{
 return(mathml(doc$doc$children))
}

mathml.default <-
#
# Attempts to create an expression from the Math ML
# document tree given to it.
# This is an example using the mathml.xml and is not
# in any way intended to be a general MathML "interpreter"
# for R/S.
#
function(children)
{
  expr <- list()
  for(i in children) {
    if(class(i) == "XMLComment")
      next

    expr <- c(expr, mathml(i))
  }

 return(expr)
}


mergeMathML <-
#
# This takes a list of objects previously converted to R terms
# from MathML and aggregates them by collapsing elements
# such as 
#      term operator term
# into R calls.
#
# see mathml.XMLNode
#
function(els)
{
#cat("Merging",length(els));
#print(els)
 ans <- list() 
 more <- T
 ctr <- 1
 while(more) {
  i <- els[[ctr]]
   if(inherits(i, "MathMLOperator")) {
     ans <- c(i, ans, els[[ctr+1]])
     mode(ans) <- "call"
     ctr <- ctr + 1
   } else if(inherits(i,"MathMLGroup")) {
#print("MathMLGroup")
     ans <- c(ans, i)
     mode(ans) <- "call"
   } else
    ans <- c(ans, i)
  
  ctr <- ctr + 1
  more <- (ctr <= length(els))
 }
#cat("Merged: "); print(ans)
 return(ans)
}



mathml.XMLNode <-
#
# Interprets a MathML node and converts it
# to an R expression term. This handles tags
# such as mi, mo, mn, msup, mfrac, mrow, mfenced, 
# msqrt, mroot
#
# Other tags include:
#   msub
#   msubsup
#   munder
#   mover
#   munderover
#   mmultiscripts
#
#   mtable
#   mtr
#   mtd
#
#  set, interval, vector, matrix
#  cn
#  matrix, matrixrow
#  transpose


# Attributes for mfenced: open, close  "["

function(node)
{
 nm <- name(node)

 if(nm == "msup" || nm == "mfrac") {
   op <- switch(nm, msup="^", mfrac="/")
    a <- mathml(node$children[[1]])
    b <- mathml(node$children[[2]])
    expr <- list(as.name(op), a, b)
    mode(expr) <- "call"
    val <- expr
 } else if(nm == "mi" || nm == "ci") {
     # display in italics
   if(!is.null(node$children[[1]]$value))
     val <- as.name(node$children[[1]]$value)
 } else if(nm == "mo") {
    if(inherits(node$children[[1]],"XMLEntityRef")) {
      # node$children[[1]]$value   
      val <- as.name("*")
      class(val) <- "MathMLOperator"
    } else {
      # operator
    tmp <- node$children[[1]]$value
    if(!is.null(tmp)) {
      if(tmp == "=") {
          # or we could use "=="
          # to indicate equality, not assignment.
        tmp <- "<-" 
      }
      val <- as.name(tmp)
      class(val) <- "MathMLOperator"
    }
   }
 } else if(nm == "text") {
     val <- node$value
 } else if(nm == "matrix"){
    val <- mathml.matrix(node)
 } else if(nm == "vector"){
    val <- mathml.vector(node)
 } else if(nm == "mn"  || nm == "cn") {
       # number tag.
   if(!is.null(node$children[[1]]$value))
     val <- as.numeric(node$children[[1]]$value)
 } else if(nm == "mrow" || nm == "mfenced" || nm == "msqrt" || nm == "mroot") {
    # group of elements (displayed in a single row)
   ans <- vector("list", length(node$children))
   ctr <- 1
   for(i in node$children) {
    ans[[ctr]] <- mathml(i)
#cat(ctr,i$name,length(ans),"\n")
    ctr <- ctr + 1
   }

   ans <- mergeMathML(ans)

    # if this is an mfenced, msqrt or mroot element, add the 
    # enclosing parentheses or function call.
    #   ....
   if(nm == "msqrt") {
    ans <- c(as.name("sqrt"), ans)
    mode(ans) <- "call"
   } else if(nm == "mfenced") {
     class(ans) <- "MathMLGroup"
   }

   val <- ans
 } else if(nm == "reln") {
   val <- mathml(node$children)
   mode(val) <- "call"
 } else if(nm == "eq") {
    val <- as.name("==")
 } else if(nm == "apply") {
   val <- mathml(node$children)
cat("apply:",length(val),"\n")
print(val)

   mode(val) <- "call"
 } else if(nm == "times") {
   val <- as.name("%*%")
 } else if(nm == "transpose") {
   val <- as.name("t")
 }
 return(val)
}

mathml.matrix <-
#
#
#
#
function(node)
{
 m <- matrix(character(1), length(node$children), length(node$children[[1]]$children))
 i <- 1
 for(row in  node$children) {
  j <- 1
  for(cell in row$children) {
    tmp <- mathml(cell)
    m[i,j] <- as.character((tmp))
    j <- j + 1
  }
   i <- i + 1
 }

print(m)
 return(m)
}


mathml.vector <-
function(node)
{
 ans <- character(length(node$children))

 for(i in 1:length(node$children)) {
  tmp <- mathml(node$children[[i]])
  ans[i] <- as.character(tmp)
 }

print(ans)
 return(ans)
}