# R/sum.r In shabbychef/madness: Automatic Differentiation of Multivariate Operations

```# /usr/bin/r
#
# Author: Steven E. Pav
#
# This file is part of madness.
#
# madness is free software: you can redistribute it and/or modify
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# madness is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
#
# Created: 2015.11.22
# Copyright: Steven E. Pav, 2015
# Author: Steven E. Pav <[email protected]>

#' @include AllClass.r
#' @include utils.r
NULL

#' @title Sum and Product.
#'
#' @description
#'
#' Compute sum or product of \code{madness} objects.
#'
#' @include AllClass.r
#' @param x a numeric or \code{madness} object.
#' @param na.rm logical. Should missing values (including \sQuote{NaN}) be
#' removed?
#' @param ... ignored here.
#' @inheritParams base::sum
#' @return a \code{madness} object representing a scalar value.
#' @name sumprod
#' @template etc
#' @examples
#' xv <- matrix(rnorm(5*5),ncol=5)
#' prod(xv)
#' sum(xv)
NULL
#' @rdname sumprod
#' @aliases sum
function(x, ..., na.rm = FALSE) {
xtag <- x@xtag
val <- sum(x@val,na.rm=na.rm)

if (na.rm) {
isok <- !(is.na(x@val) | is.nan(x@val))
dvdx <- colSums(x@dvdx[which(isok),,drop=FALSE])
} else {
dvdx <- colSums(x@dvdx)
}
dvdx <- matrix(dvdx,nrow=1)

vtag <- paste0('sum(',x@vtag,', na.rm=',na.rm,')')
varx <- x@varx

retv <- new("madness", val=val, dvdx=dvdx, vtag=vtag, xtag=xtag, varx=varx)
retv <- retv + sum(...,na.rm=na.rm)
retv
})
#' @rdname sumprod
#' @aliases prod
function(x, ..., na.rm = FALSE) {
xtag <- x@xtag
if (na.rm) {
valiii <- which(!(is.na(x@val) | is.nan(x@val)))
} else {
valiii <- seq_len(length(x@val))
}
cval <- c(1,cumprod(x@val[valiii]))
val <- cval[length(cval)]
dvdx <- matrix(0,nrow=1,ncol=ncol(x@dvdx))
for (jjj in seq_along(valiii)) {
vi <- valiii[jjj]
dvdx <- x@val[vi] * dvdx + cval[jjj] * x@dvdx[vi,,drop=FALSE]
}
dvdx <- matrix(dvdx,nrow=1)

vtag <- paste0('prod(',x@vtag,', na.rm=',na.rm,')')
varx <- x@varx

retv <- new("madness", val=val, dvdx=dvdx, vtag=vtag, xtag=xtag, varx=varx)
retv <- retv * prod(...,na.rm=na.rm)
retv
})

#for vim modeline: (do not edit)
# vim:fdm=marker:fmr=FOLDUP,UNFOLD:cms=#%s:syn=r:ft=r
```
shabbychef/madness documentation built on May 29, 2019, 8:06 p.m.