Re: [R] integrate with vector arguments
On Feb 27, 2015, at 4:49 AM, marKo mton...@ffri.hr wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gee. That simple. I knew that! Thanks a lot. Essentially, I needed only the diagonal elements. Easily solved by: diag(outer( X=v1,Y=v2, FUN= fV) I am sure that there are simpler options, but that works like a charm. If I had understood that you wanted only the values with v1 and v2 paired, then I would have suggested using apply: mapply(fV, v1, v2) [1] 0.833 1.667 2.500 3.333 4.167 — David. Thanks a lot. Cheers, Marko On 02/27/2015 06:49 AM, David Winsemius wrote: On Feb 26, 2015, at 1:49 PM, marKo mton...@ffri.hr wrote: v1-c(1:5) v2-c(1:5) f1-function (x) {v1*x+v2*x^2} The problem is that integrate(f1, 0, 1) does not work. I does not, even if a pas the arguments (v1, v2) f1-function (x, v1, v2) {v1*x+v2*x^2} or if i try to vectorize the function f1-Vectorize(function(x, v1, v2){v1*x+v2*x^2}, vectorize.args=c(v1, v2)) integrate(f1, 0, 1) gives an error: f1-function (X, Y) integrate( function(x) {X*x+Y*x^2}, 0, 1)$value fV-Vectorize(f1) outer( X=v1,Y=v2, FUN= fV) [,1] [,2] [,3] [,4] [,5] [1,] 0.833 1.17 1.5 1.83 2.17 [2,] 1.333 1.67 2.0 2.33 2.67 [3,] 1.833 2.17 2.5 2.83 3.17 [4,] 2.333 2.67 3.0 3.33 3.67 [5,] 2.833 3.17 3.5 3.83 4.17 — David Winsemius, MD Alameda, CA, USA -BEGIN PGP SIGNATURE- Version: GnuPG v1 iQIcBAEBAgAGBQJU8D2hAAoJEJcj4KySkkQsblsQAK2cpOG/qsrXP6vK9Zq3UMkc 6jNQ9PQqvT1H8WyR8rNTY1Bis7npxgNh0avRYOrsRf1D3u9frNjBAhT2v0mMW+Uk LRvHpER1EnIZhdFcmE7uOUNsaU/IquicVVWAXgVVu2/yUW8nJZ4FQ2wkKDLlTY97 dyNMj5i1r5DWBtfUbCtTiSJz1G16DXiJBsk0Zi1Z9zT71evOh2tIfztv1qSSTOLJ gjWbpefYThypPq/170+Eqv3BOigjQd1ljeyXVgGrs/vMTX6a9ZL5KDUYsR7QnhmH ZiHNUTcUpAFb3QGLNVM6ULd7GfzDKgG7rH8JwokYzi/IIsm5aTNfY7dUCGySx+dL 4tOQKe6LjFGIfqPHeZXXs1ErYrIs7L//z+n9ZfHKT0RH7zAoakrzkvFxsBx2LU7z Wziv3/eMN65WOPFpH2puaUQrandbmpDujuZS0PfGBc5/hrIpeCejm7LfHocck6MT v+J4o05+a3jMSdZdtbNXv6q5z+tXaEdThKz77VmFwMQWzFto8QFbe5dOKP9EO6Vn 9+u0gSswtE43PXkn3Egj6HmhrTkREc3lnHsh/0E32+g4UsWxVlyLvfCrm9s4zKOZ fGjIpaUPUp2AkNGAa8BhLqO6LJVaU2/wCvcYIRAbv1Uck2XK9lppKsvFEUuRz9Uk kPaGAS9OJgsUS8kMVpMf =v7j4 -END PGP SIGNATURE- David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integrate with vector arguments
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Yes. That's it. Thanks, a lot, really. Marko On 02/27/2015 02:46 PM, David Winsemius wrote: On Feb 27, 2015, at 4:49 AM, marKo mton...@ffri.hr wrote: Gee. That simple. I knew that! Thanks a lot. Essentially, I needed only the diagonal elements. Easily solved by: diag(outer( X=v1,Y=v2, FUN= fV) I am sure that there are simpler options, but that works like a charm. If I had understood that you wanted only the values with v1 and v2 paired, then I would have suggested using apply: mapply(fV, v1, v2) [1] 0.833 1.667 2.500 3.333 4.167 — David. Thanks a lot. Cheers, Marko On 02/27/2015 06:49 AM, David Winsemius wrote: On Feb 26, 2015, at 1:49 PM, marKo mton...@ffri.hr wrote: v1-c(1:5) v2-c(1:5) f1-function (x) {v1*x+v2*x^2} The problem is that integrate(f1, 0, 1) does not work. I does not, even if a pas the arguments (v1, v2) f1-function (x, v1, v2) {v1*x+v2*x^2} or if i try to vectorize the function f1-Vectorize(function(x, v1, v2){v1*x+v2*x^2}, vectorize.args=c(v1, v2)) integrate(f1, 0, 1) gives an error: f1-function (X, Y) integrate( function(x) {X*x+Y*x^2}, 0, 1)$value fV-Vectorize(f1) outer( X=v1,Y=v2, FUN= fV) [,1] [,2] [,3] [,4] [,5] [1,] 0.833 1.17 1.5 1.83 2.17 [2,] 1.333 1.67 2.0 2.33 2.67 [3,] 1.833 2.17 2.5 2.83 3.17 [4,] 2.333 2.67 3.0 3.33 3.67 [5,] 2.833 3.17 3.5 3.83 4.17 — David Winsemius, MD Alameda, CA, USA David Winsemius, MD Alameda, CA, USA -BEGIN PGP SIGNATURE- Version: GnuPG v1 iQIcBAEBAgAGBQJU8HXQAAoJEJcj4KySkkQsDMUP/i2vCLuf21yrMIFotBehOcF8 XpN3gRWAvRAe9Thd4mpIzSj+5woQLJH/Rp8bF2ClXh0HaO3j62fn+RGJeKBEDS0g gxqkPFapm0ug6qEIYHvguHVLTMuAEWgzU+fpTY955jAwOYvrkHfWJ9XGqfik7R1o jBfbquq/4kyUqur1iBsHp4RZXkXEUfuk5lLPv03Tl7B6TOift0M/yJf5PAzi0VvY 2jhDpAvSJ6JSjwx+R+xnDkXhkSNMQAoiZuabuwVbD9dqA7T5dxreieAbu+YcUo4E FF96A6G7gfIgneCdX3i/2i632MjTfDTe1eDB5YYATf5GnmzYB1K0ZH6Egr6sB4/7 /0wvObJHWJfc5qZQpqJyk8mEy54Igm/jko75yaQ7EIHWbDEcm30AyC0b0/7qTels w2Tie3T4KvnjfSaPSz65AW7ip8bWYy3QpgNTzpQEn1zNjWSJ3lQYOOvWwFhr67YN axft3nHRpthOUWk5lZgLJ5Nmx0ILB/NxYIh/nj2uUypsYYvvMNqKTg+QRNEICm51 bace0rfvPTl4UaCCrtFtydN7awv8/weRCMqUtSZccxZ9Hvkli6inIWH0WDK9/xg3 HxPgv8BEF9wJtfHa8HWtDEG7pqUT5byhd3UVtxSkH+RYZl9iGu/Vat/a3Ikj+6Lb i9lIjaTylqlAy//m4S/Q =EH2y -END PGP SIGNATURE- __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integrate with vector arguments
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gee. That simple. I knew that! Thanks a lot. Essentially, I needed only the diagonal elements. Easily solved by: diag(outer( X=v1,Y=v2, FUN= fV) I am sure that there are simpler options, but that works like a charm. Thanks a lot. Cheers, Marko On 02/27/2015 06:49 AM, David Winsemius wrote: On Feb 26, 2015, at 1:49 PM, marKo mton...@ffri.hr wrote: v1-c(1:5) v2-c(1:5) f1-function (x) {v1*x+v2*x^2} The problem is that integrate(f1, 0, 1) does not work. I does not, even if a pas the arguments (v1, v2) f1-function (x, v1, v2) {v1*x+v2*x^2} or if i try to vectorize the function f1-Vectorize(function(x, v1, v2){v1*x+v2*x^2}, vectorize.args=c(v1, v2)) integrate(f1, 0, 1) gives an error: f1-function (X, Y) integrate( function(x) {X*x+Y*x^2}, 0, 1)$value fV-Vectorize(f1) outer( X=v1,Y=v2, FUN= fV) [,1] [,2] [,3] [,4] [,5] [1,] 0.833 1.17 1.5 1.83 2.17 [2,] 1.333 1.67 2.0 2.33 2.67 [3,] 1.833 2.17 2.5 2.83 3.17 [4,] 2.333 2.67 3.0 3.33 3.67 [5,] 2.833 3.17 3.5 3.83 4.17 — David Winsemius, MD Alameda, CA, USA -BEGIN PGP SIGNATURE- Version: GnuPG v1 iQIcBAEBAgAGBQJU8D2hAAoJEJcj4KySkkQsblsQAK2cpOG/qsrXP6vK9Zq3UMkc 6jNQ9PQqvT1H8WyR8rNTY1Bis7npxgNh0avRYOrsRf1D3u9frNjBAhT2v0mMW+Uk LRvHpER1EnIZhdFcmE7uOUNsaU/IquicVVWAXgVVu2/yUW8nJZ4FQ2wkKDLlTY97 dyNMj5i1r5DWBtfUbCtTiSJz1G16DXiJBsk0Zi1Z9zT71evOh2tIfztv1qSSTOLJ gjWbpefYThypPq/170+Eqv3BOigjQd1ljeyXVgGrs/vMTX6a9ZL5KDUYsR7QnhmH ZiHNUTcUpAFb3QGLNVM6ULd7GfzDKgG7rH8JwokYzi/IIsm5aTNfY7dUCGySx+dL 4tOQKe6LjFGIfqPHeZXXs1ErYrIs7L//z+n9ZfHKT0RH7zAoakrzkvFxsBx2LU7z Wziv3/eMN65WOPFpH2puaUQrandbmpDujuZS0PfGBc5/hrIpeCejm7LfHocck6MT v+J4o05+a3jMSdZdtbNXv6q5z+tXaEdThKz77VmFwMQWzFto8QFbe5dOKP9EO6Vn 9+u0gSswtE43PXkn3Egj6HmhrTkREc3lnHsh/0E32+g4UsWxVlyLvfCrm9s4zKOZ fGjIpaUPUp2AkNGAa8BhLqO6LJVaU2/wCvcYIRAbv1Uck2XK9lppKsvFEUuRz9Uk kPaGAS9OJgsUS8kMVpMf =v7j4 -END PGP SIGNATURE- __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integrate with vector arguments
Hi, The following works. f2 function(z) { f1 - function(t) { z*t + z*t^2 } return(f1) } sapply(1:5,function(x)integrate(f2(x),0,1)$value) [1] 0.83 1.67 2.50 3.33 4.17 -- View this message in context: http://r.789695.n4.nabble.com/integrate-with-vector-arguments-tp4703906p4703925.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integrate with vector arguments
On Feb 26, 2015, at 1:49 PM, marKo mton...@ffri.hr wrote: v1-c(1:5) v2-c(1:5) f1-function (x) {v1*x+v2*x^2} The problem is that integrate(f1, 0, 1) does not work. I does not, even if a pas the arguments (v1, v2) f1-function (x, v1, v2) {v1*x+v2*x^2} or if i try to vectorize the function f1-Vectorize(function(x, v1, v2){v1*x+v2*x^2}, vectorize.args=c(v1, v2)) integrate(f1, 0, 1) gives an error: f1-function (X, Y) integrate( function(x) {X*x+Y*x^2}, 0, 1)$value fV-Vectorize(f1) outer( X=v1,Y=v2, FUN= fV) [,1] [,2] [,3] [,4] [,5] [1,] 0.833 1.17 1.5 1.83 2.17 [2,] 1.333 1.67 2.0 2.33 2.67 [3,] 1.833 2.17 2.5 2.83 3.17 [4,] 2.333 2.67 3.0 3.33 3.67 [5,] 2.833 3.17 3.5 3.83 4.17 — David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] integrate with vector arguments
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 I'm a bit stuck. I have to integrate a series of polynomial functions with vector arguments. v1-c(1:5) v2-c(1:5) f1-function (x) {v1*x+v2*x^2} The problem is that integrate(f1, 0, 1) does not work. I does not, even if a pas the arguments (v1, v2) f1-function (x, v1, v2) {v1*x+v2*x^2} or if i try to vectorize the function f1-Vectorize(function(x, v1, v2){v1*x+v2*x^2}, vectorize.args=c(v1, v2)) integrate(f1, 0, 1) gives an error: Error in integrate(f1, 0, 1) : evaluation of function gave a result of wrong length Any help will be greatly appreciated. Thanks, Marko -BEGIN PGP SIGNATURE- Version: GnuPG v1 iQIcBAEBAgAGBQJU72qnAAoJEJcj4KySkkQsLAkP/R7DvO0GiZDRrtHgDna/2xj+ XJd8G/gGfe029lVjg+3i6wfKfZ9CoRH+kHEVnT0/SRYcSAeRu3/fys11sjEgVGnl a/Go167YRYfDkP/OrY4jKtlULySeiGBxNJwKmk1oCidoodk2mejWdPQ61tBj6ozF sA+Bzoi7Exh2pp88Eks4+Ynz+Toi8Ck1hItV60kP9yOMSBsIPVLw53lGXDfOshzM zLcFbHM5hyjmt/BQvyaBm3E822YEJgcDQN3nedjQgwThJuEyig2TXHAvyEZcdBWD H8Py0b5/TBdmxqJQ3EqYyBFmPxeFuhO4ZS22IhP+rqPJ51EZnfqG6DRBHHLqQ9rX ZnYJN8ryqDVMOrYHn6j3dNd/m7C/YWmrY8gjArv8WxRsX+kX+DAgbRmiw/43BXNG Y2Jco5dChWBrXQDR3FMoJWBTWjvwgPfP06hnwjrJT1uJZQLPUzhdrIxyHxbhsW0A UeiRqNiPjE9YpKrFGn9Itg1tXk35yrPrNmmj1nzIzaHejMzT8zf0X2pJAygAYyk3 +mrEgwkB31GOt2mUqqFzDxgDHASaSTPlskviIVJ9klcs7ViWYSy5ARiF4/ptbluE CTny7dVj/AoXq8dC8TxghOT1QSnPVy7ceb6fCep7LxJDWlFqTEM0LCbL7Ql78yzP +Em5gaikzPGbJ7uvVKIG =7J6P -END PGP SIGNATURE- __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integrate with vector arguments
marKo mtoncic at ffri.hr writes: I'm a bit stuck. I have to integrate a series of polynomial functions with vector arguments. v1-c(1:5) v2-c(1:5) f1-function (x) {v1*x+v2*x^2} The problem is that integrate(f1, 0, 1) does not work. The point is not that there are vector arguments, but that your function is vector-valued and so the generated error message below rightly says evaluation of function gave a result of wrong length. You could integrate each dimension separately or, e.g., you use quadv() from package 'pracma' which handles vector-valued functions: v1 - v2 - 1:5 f1-function (x) {v1*x+v2*x^2} library(pracma) quadv(f1, 0, 1, tol=1e-10) $Q [1] 0.833 1.667 2.500 3.333 4.167 $fcnt [1] 13 $estim.prec [1] 0.03 quadv() employs an adaptive Simpson quadrature where the recursion is applied to all components at once. I does not, even if a pas the arguments (v1, v2) f1-function (x, v1, v2) {v1*x+v2*x^2} or if i try to vectorize the function f1-Vectorize(function(x, v1, v2){v1*x+v2*x^2}, vectorize.args=c(v1, v2)) integrate(f1, 0, 1) gives an error: Error in integrate(f1, 0, 1) : evaluation of function gave a result of wrong length Any help will be greatly appreciated. Thanks, Marko __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.