R Under development (unstable) (2023-10-23 r85401 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # tfnchk.R > ## author: John C. Nash > # rm(list=ls()) > cat("Show how fnchk works\n") Show how fnchk works > require(optimx) Loading required package: optimx > sessionInfo() R Under development (unstable) (2023-10-23 r85401 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows Server 2022 x64 (build 20348) Matrix products: default locale: [1] LC_COLLATE=C LC_CTYPE=German_Germany.utf8 [3] LC_MONETARY=C LC_NUMERIC=C [5] LC_TIME=C time zone: Europe/Berlin tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] optimx_2023-10.21 loaded via a namespace (and not attached): [1] compiler_4.4.0 nloptr_2.0.3 numDeriv_2016.8-1.1 [4] pracma_2.4.2 > > # Want to illustrate each case. > # Ben Bolker idea for a function that is NOT scalar > > benbad<-function(x, y){ + # y may be provided with different structures + f<-(x-y)^2 + } # very simple, but ... > > y<-1:10 > x<-c(1) > cat("test benbad() with y=1:10, x=c(1)\n") test benbad() with y=1:10, x=c(1) > fc01<-fnchk(x, benbad, trace=1, y) Function value at supplied parameters = [1] 0 1 4 9 16 25 36 49 64 81 num [1:10] 0 1 4 9 16 25 36 49 64 81 NULL [1] TRUE Function evaluation returns a vector not a scalar Function evaluation returned non-numeric value Function evaluation returned Inf or NA (non-computable) Function at given point= NA > print(fc01) $fval [1] NA $infeasible [1] TRUE $excode [1] -1 $msg [1] "Function evaluation returned Inf or NA (non-computable)" > > y<-as.vector(y) > cat("test benbad() with y=as.vector(1:10), x=c(1)\n") test benbad() with y=as.vector(1:10), x=c(1) > fc02<-fnchk(x, benbad, trace=1, y) Function value at supplied parameters = [1] 0 1 4 9 16 25 36 49 64 81 num [1:10] 0 1 4 9 16 25 36 49 64 81 NULL [1] TRUE Function evaluation returns a vector not a scalar Function evaluation returned non-numeric value Function evaluation returned Inf or NA (non-computable) Function at given point= NA > print(fc02) $fval [1] NA $infeasible [1] TRUE $excode [1] -1 $msg [1] "Function evaluation returned Inf or NA (non-computable)" > > y<-as.matrix(y) > cat("test benbad() with y=as.matrix(1:10), x=c(1)\n") test benbad() with y=as.matrix(1:10), x=c(1) > fc03<-fnchk(x, benbad, trace=1, y) Function value at supplied parameters = [,1] [1,] 0 [2,] 1 [3,] 4 [4,] 9 [5,] 16 [6,] 25 [7,] 36 [8,] 49 [9,] 64 [10,] 81 num [1:10, 1] 0 1 4 9 16 25 36 49 64 81 NULL [1] FALSE Function evaluation returns a matrix list not a scalar Function evaluation returned non-numeric value Function evaluation returned Inf or NA (non-computable) Function at given point= NA > print(fc03) $fval [1] NA $infeasible [1] TRUE $excode [1] -1 $msg [1] "Function evaluation returned Inf or NA (non-computable)" > > y<-as.array(y) > cat("test benbad() with y=as.array(1:10), x=c(1)\n") test benbad() with y=as.array(1:10), x=c(1) > fc04<-fnchk(x, benbad, trace=1, y) Function value at supplied parameters = [,1] [1,] 0 [2,] 1 [3,] 4 [4,] 9 [5,] 16 [6,] 25 [7,] 36 [8,] 49 [9,] 64 [10,] 81 num [1:10, 1] 0 1 4 9 16 25 36 49 64 81 NULL [1] FALSE Function evaluation returns a matrix list not a scalar Function evaluation returned non-numeric value Function evaluation returned Inf or NA (non-computable) Function at given point= NA > print(fc04) $fval [1] NA $infeasible [1] TRUE $excode [1] -1 $msg [1] "Function evaluation returned Inf or NA (non-computable)" > > y<-"This is a string" > cat("test benbad() with y a string, x=c(1)\n") test benbad() with y a string, x=c(1) > fc05<-fnchk(x, benbad, trace=1, y) Error in x - y : non-numeric argument to binary operator Function value at supplied parameters =[1] NA attr(,"inadmissible") [1] TRUE logi NA - attr(*, "inadmissible")= logi TRUE NULL [1] FALSE Function evaluation returns INADMISSIBLE Function evaluation returned non-numeric value Function evaluation returned Inf or NA (non-computable) Function at given point= NA > print(fc05) $fval [1] NA $infeasible [1] TRUE $excode [1] -1 $msg [1] "Function evaluation returned Inf or NA (non-computable)" > > fr <- function(x) { ## Rosenbrock Banana function + x1 <- x[1] + x2 <- x[2] + 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 + } > xtrad<-c(-1.2,1) > ros1<-fnchk(xtrad, fr, trace=1) Function value at supplied parameters =[1] 24.2 num 24.2 NULL [1] TRUE Function at given point= 24.2 > print(ros1) $fval [1] 24.2 $infeasible [1] FALSE $excode [1] 0 $msg [1] "fnchk OK" > > > proc.time() user system elapsed 0.18 0.06 0.20