{"id":243,"date":"2010-08-31T14:31:17","date_gmt":"2010-08-31T14:31:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/08\/31\/combining-functions\/"},"modified":"2010-08-30T11:17:36","modified_gmt":"2010-08-30T11:17:36","slug":"combining-functions","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/08\/31\/combining-functions\/","title":{"rendered":"Combining Functions"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>For a lot of calculations, it makes sense to combine operations into a single expression.  You can sometimes do this easily\r\n         when you have function handles and you generate one final function that you can apply.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Function Composition<\/a><\/li>\r\n         <li><a href=\"#3\">Other Combinations.<\/a><\/li>\r\n         <li><a href=\"#10\">Do You Combine Functions?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Function Composition<a name=\"1\"><\/a><\/h3>\r\n   <p>Suppose you want to call <tt>g(f(x))<\/tt>, where the output of the function <tt>f<\/tt> is the input for the function <tt>g<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = @(x) sin(x);\r\ng = @(x) cos(x);\r\nfCompg = @(x) g(f(x));<\/pre><p>% Test the composition.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = 0:pi\/100:pi\/2;\r\nfx = f(x);\r\ngfx = g(fx);\r\ngfx2 = fCompg(x);\r\ngfsEqual = isequal(gfx,gfx2)<\/pre><pre style=\"font-style:oblique\">gfsEqual =\r\n     1\r\n<\/pre><h3>Other Combinations.<a name=\"3\"><\/a><\/h3>\r\n   <p>Suppose instead you want to add some functions together under certain circumstances.  Sometimes, but not always.  It's messy\r\n      if you don't commit to evaluating a function handle with a fixed name.  So, here's a pattern you can use to achieve this effect.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">myfunc = @(x) sin(x);\r\nmn = @(x) mean(x);\r\nthree = @(x) 3+zeros(size(x));\r\nsubtractMean = false;\r\nadd3 = true;<\/pre><p>Suppose I want to add a constant offset only.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">if<\/span> subtractMean\r\n    myfunc = @(x) bsxfun(@minus, myfunc(x), mn(x));\r\n<span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">if<\/span> add3\r\n    myfunc = @(x) myfunc(x) + three(x);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Try this on the data.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mf3mean = myfunc(x);<\/pre><p>Now, regardless of which functions we combine with the original function, we have a function handle to evaluate.  You can\r\n      use this to allow users to specify additional information as you build up an appropriate function for your calculation.\r\n   <\/p>\r\n   <p>Let's try a different combination.  Let's suppose I want the mean value to be around 3. I can first remove the mean and then\r\n      add the offset.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">myfunc = @(x) sin(x);\r\nsubtractMean = true;\r\nadd3 = true;\r\n<span style=\"color: #0000FF\">if<\/span> subtractMean\r\n    myfunc = @(x) bsxfun(@minus, myfunc(x), mn(x));\r\n<span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">if<\/span> add3\r\n    myfunc = @(x) myfunc(x) + three(x);\r\n<span style=\"color: #0000FF\">end<\/span>\r\nmf3nomean = myfunc(x);<\/pre><p>Notice the order of operations matters here.  If I removed the mean after adding the constant, the final mean should be 0.\r\n       If I remove it before adding the offset, I have no reason to expect that.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mean(mf3nomean)\r\nmean(mf3mean)<\/pre><pre style=\"font-style:oblique\">ans =\r\n       2.8485\r\nans =\r\n       3.6339\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = plot(mf3nomean);\r\nhold <span style=\"color: #A020F0\">all<\/span>\r\nh(2) = plot(mf3mean);\r\nhold <span style=\"color: #A020F0\">off<\/span>\r\nlegend(h,{<span style=\"color: #A020F0\">'mf3nomean'<\/span>, <span style=\"color: #A020F0\">'mf3mean'<\/span>})<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/243\/fcompose_01.png\"> <h3>Do You Combine Functions?<a name=\"10\"><\/a><\/h3>\r\n   <p>Do you have situations in which you conditionally apply functions?  Would this technique help you build up the functions you\r\n      want more easily?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=243#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_9bf5f957718e42eb9e1d30aef28c8062() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='9bf5f957718e42eb9e1d30aef28c8062 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9bf5f957718e42eb9e1d30aef28c8062';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2010 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_9bf5f957718e42eb9e1d30aef28c8062()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\n9bf5f957718e42eb9e1d30aef28c8062 ##### SOURCE BEGIN #####\r\n%% Combining Functions\r\n% For a lot of calculations, it makes sense to combine operations into a\r\n% single expression.  You can sometimes do this easily when you have\r\n% function handles and you generate one final function that you can apply.\r\n%% Function Composition\r\n% Suppose you want to call |g(f(x))|, where the output of the function |f|\r\n% is the input for the function |g|.\r\nf = @(x) sin(x);\r\ng = @(x) cos(x);\r\nfCompg = @(x) g(f(x));\r\n\r\n%%\r\n% % Test the composition.\r\nx = 0:pi\/100:pi\/2;\r\nfx = f(x);\r\ngfx = g(fx);\r\ngfx2 = fCompg(x);\r\ngfsEqual = isequal(gfx,gfx2)\r\n\r\n%% Other Combinations.\r\n% Suppose instead you want to add some functions together under certain\r\n% circumstances.  Sometimes, but not always.  It's messy if you don't\r\n% commit to evaluating a function handle with a fixed name.  So, here's a\r\n% pattern you can use to achieve this effect.\r\nmyfunc = @(x) sin(x);\r\nmn = @(x) mean(x);\r\nthree = @(x) 3+zeros(size(x)); \r\nsubtractMean = false;\r\nadd3 = true;\r\n\r\n%%\r\n% Suppose I want to add a constant offset only.\r\nif subtractMean\r\n    myfunc = @(x) bsxfun(@minus, myfunc(x), mn(x));\r\nend\r\nif add3\r\n    myfunc = @(x) myfunc(x) + three(x);\r\nend\r\n\r\n%%\r\n% Try this on the data.\r\nmf3mean = myfunc(x);\r\n%%\r\n% Now, regardless of which functions we combine with the original function,\r\n% we have a function handle to evaluate.  You can use this to allow users\r\n% to specify additional information as you build up an appropriate function\r\n% for your calculation.\r\n%% \r\n% Let's try a different combination.  Let's suppose I want the mean value\r\n% to be around 3. I can first remove the mean and then add the offset.\r\nmyfunc = @(x) sin(x);\r\nsubtractMean = true;\r\nadd3 = true;\r\nif subtractMean\r\n    myfunc = @(x) bsxfun(@minus, myfunc(x), mn(x));\r\nend\r\nif add3\r\n    myfunc = @(x) myfunc(x) + three(x);\r\nend\r\nmf3nomean = myfunc(x);\r\n%%\r\n% Notice the order of operations matters here.  If I removed the mean after\r\n% adding the constant, the final mean should be 0.  If I remove it before\r\n% adding the offset, I have no reason to expect that.\r\n\r\nmean(mf3nomean)\r\nmean(mf3mean)\r\n%%\r\nh = plot(mf3nomean);\r\nhold all\r\nh(2) = plot(mf3mean);\r\nhold off\r\nlegend(h,{'mf3nomean', 'mf3mean'})\r\n%% Do You Combine Functions?\r\n% Do you have situations in which you conditionally apply functions?  Would\r\n% this technique help you build up the functions you want more easily?  Let\r\n% me know <https:\/\/blogs.mathworks.com\/loren\/?p=243#respond here>.\r\n##### SOURCE END ##### 9bf5f957718e42eb9e1d30aef28c8062\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      For a lot of calculations, it makes sense to combine operations into a single expression.  You can sometimes do this easily\r\n         when you have function handles and you generate one... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/08\/31\/combining-functions\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/243"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}