alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (testFunctions)

This example Commons Math source code file (testFunctions) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

apache, asf, asf, displays, failed, license, license, of, see, see, succeeded, true, you, you

The Commons Math testFunctions source code

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#------------------------------------------------------------------------------
#
# Utility functions used in R comparison tests.
#
#------------------------------------------------------------------------------
# Global constants
#------------------------------------------------------------------------------
WIDTH <- 80                    # screen size constant for display functions
SUCCEEDED <- "SUCCEEDED"
FAILED <- "FAILED"
options(digits=12)             # display 12 digits throughout
#------------------------------------------------------------------------------
# Comparison functions
#------------------------------------------------------------------------------
# Tests to see if <expected> and  are within  of
# one another in the sup norm.
#
# Returns 1 if no pair of corresponding non-NULL, non-NaN, non-na entries
# differs by more than abs and NULLs, NaNs, na's correspond;
# otherwise displays <message> and returns 0.
# Works for both vectors and scalar values.
#
assertEquals <- function(expected, observed, tol, message) {
    failed <- 0
    if (any(is.na(observed) != is.na(expected))) {
        failed <- 1
    }
    if (any(is.null(observed) != is.null(expected))) {
        failed <- 1
    }
    if (any(is.nan(expected) != is.nan(observed))) {
        failed <- 1
    }
    if (any(is.na(expected) != is.na(observed))) {
        failed <- 1
    }
    if (!failed) {
        if(any(abs(observed - expected) > tol, na.rm = TRUE)) {
            failed <- 1
        }
    }
    if (failed) {
        cat("FAILURE: ",message,"\n")
        cat("EXPECTED: ",expected,"\n")
        cat("OBSERVED: ",observed,"\n")
        cat("DIFF: ",observed - expected,"\n")
        cat("TOLERANCE: ",tol,"\n")
    }  
    return(!failed)
}
#------------------------------------------------------------------------------
# Display functions
#------------------------------------------------------------------------------
# Displays n-col dashed line.
#
displayDashes <- function(n) {
    cat(rep("-",n),"\n",sep='')
    return(1)
}
#------------------------------------------------------------------------------
# Displays <start>...... with enough dots in between to make  cols, 
# followed by a new line character. Blows up if <start> is longer than
# <n> cols by itself.
#
# Expects <start> and  to be strings (character vectors).
# 
displayPadded <- function(start, end, n) {
    len = sum(nchar(start)) + sum(nchar(end))
    cat(start, rep(".", n - len), end, "\n",sep='')
    return(1)
}

Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math testFunctions source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.