{"id":219,"date":"2010-02-24T19:15:17","date_gmt":"2010-02-24T19:15:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/02\/24\/ensuring-positive-values-part-2\/"},"modified":"2010-02-17T19:16:20","modified_gmt":"2010-02-17T19:16:20","slug":"ensuring-positive-values-part-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/02\/24\/ensuring-positive-values-part-2\/","title":{"rendered":"Ensuring Positive Values &#8211; Part 2"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>My <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=218\">last post<\/a> started the discussion about how to ensure data values met certain constraints. The solutions I talked about were ones where\r\n         you checked at certain places during a calculation, but the constraints were not continuously enforced. If I want to ensure\r\n         that values are <b>always<\/b> appropriate, I might create my own class and make sure values can never go sour.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Starting Positive<\/a><\/li>\r\n         <li><a href=\"#7\">Go Negative<\/a><\/li>\r\n         <li><a href=\"#9\">The Class<\/a><\/li>\r\n         <li><a href=\"#11\">How Do You Enforce Rules?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Starting Positive<a name=\"1\"><\/a><\/h3>\r\n   <p>Here's a very simple skeleton of such a class in action.  Let me first create an all-positive array.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">m = magic(3)<\/pre><pre style=\"font-style:oblique\">m =\r\n     8     1     6\r\n     3     5     7\r\n     4     9     2\r\n<\/pre><p>Next let me convert my array to the new class <tt>PositivelyLoren<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mpos = PositivelyLoren(m)<\/pre><pre style=\"font-style:oblique\">mpos = \r\n  PositivelyLoren\r\n\r\n  Properties:\r\n    values: [3x3 double]\r\n<\/pre><p>And let's see if we can nudge it out of good graces.  We first will try to assign a <tt>NaN<\/tt> to the <tt>(3,3)<\/tt> element.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    mpos.values(3,3) = NaN;\r\n<span style=\"color: #0000FF\">catch<\/span> ENotPos\r\n    disp(ENotPos.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Expected input to be finite.\r\n<\/pre><p>Now try a negative number.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    mpos.values(3,3) = -17;\r\n<span style=\"color: #0000FF\">catch<\/span> ENotPos\r\n    disp(ENotPos.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Expected input to be positive.\r\n<\/pre><p>I'll try <tt>Inf<\/tt> next.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    mpos.values(3,3) = Inf;\r\n<span style=\"color: #0000FF\">catch<\/span> ENotPos\r\n    disp(ENotPos.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Expected input to be finite.\r\n<\/pre><p>And finally a string.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    mpos.values(3,3) = {<span style=\"color: #A020F0\">'fred'<\/span>};\r\n<span style=\"color: #0000FF\">catch<\/span> ENotPos\r\n    disp(ENotPos.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Conversion to double from cell is not possible.\r\n<\/pre><h3>Go Negative<a name=\"7\"><\/a><\/h3>\r\n   <p>Next let me try to create one of these <tt>PositivelyLoren<\/tt> objects with a matrix that doesn't contain only positive values.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">r = randn(3)<\/pre><pre style=\"font-style:oblique\">r =\r\n    0.2916   -0.8045   -0.2437\r\n    0.1978    0.6966    0.2157\r\n    1.5877    0.8351   -1.1658\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    bp = PositivelyLoren(b);\r\n<span style=\"color: #0000FF\">catch<\/span> PLE\r\n    disp(PLE.message)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Expected input to be positive.\r\n<\/pre><h3>The Class<a name=\"9\"><\/a><\/h3>\r\n   <p>The class is <b>very<\/b> rudimentary.  You can tell that I didn't go into detail in the error message to give better diagnostic information (I could\r\n      have).  And I haven't added lots of useful methods, nor done much to contain the data well. Here's the class code.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">PositivelyLoren<\/span><\/pre><pre style=\"font-style:oblique\">\r\nclassdef PositivelyLoren\r\n    %PositivelyLoren Class values finite and positive.\r\n    %   This class contains data that is finite and positive ALWAYS.\r\n    \r\n    properties\r\n        values\r\n    end\r\n    \r\n    methods\r\n        function obj = PositivelyLoren(vals)\r\n        % PositivelyLoren constructor, for finite, positive numeric data.\r\n        \r\n        % Allow for default constructor.\r\n            if nargin &gt; 0\r\n                obj.values = vals;\r\n            end\r\n        end\r\n        function obj = set.values(obj,vals)\r\n        % Enforce finite, positive values for data.\r\n            validateattributes(vals,{'numeric'},...\r\n                {'finite','positive'});\r\n            obj.values = vals;\r\n        end\r\n    end\r\n    \r\nend\r\n\r\n\r\n<\/pre><p>The class has one property holding the data (<tt>values<\/tt>).  It's got a constructor that allows for the default constructor.  And it has the all important <tt>set.values<\/tt> method that is invoked any time any element in the obj is possibly being set.  In there, I make sure, using the <tt>validateattributes<\/tt> function discussed last time, to only allow the data to be set if it meets the constraints of finite and positive.\r\n   <\/p>\r\n   <h3>How Do You Enforce Rules?<a name=\"11\"><\/a><\/h3>\r\n   <p>Have you needed to enforce rules for certain data constructs?  How have you done so?  Did you do it in the one-off manor like\r\n      I did in the last post, or have you created your own class?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=219#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_07062a7aec67401dad793674bdce62ad() {\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='07062a7aec67401dad793674bdce62ad ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 07062a7aec67401dad793674bdce62ad';\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_07062a7aec67401dad793674bdce62ad()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\n07062a7aec67401dad793674bdce62ad ##### SOURCE BEGIN #####\r\n%% Ensuring Positive Values - Part 2\r\n% My <https:\/\/blogs.mathworks.com\/loren\/?p=218 last post> started the\r\n% discussion about how to ensure data values met certain constraints. The\r\n% solutions I talked about were ones where you checked at certain places\r\n% during a calculation, but the constraints were not continuously enforced.\r\n% If I want to ensure that values are *always* appropriate, I might create\r\n% my own class and make sure values can never go sour.  \r\n%% Starting Positive\r\n% Here's a very simple skeleton of such a class in action.  Let me first\r\n% create an all-positive array.\r\nm = magic(3)\r\n\r\n%%\r\n% Next let me convert my array to the new class |PositivelyLoren|.\r\nmpos = PositivelyLoren(m)\r\n%%\r\n% And let's see if we can nudge it out of good graces.  We first will try\r\n% to assign a |NaN| to the |(3,3)| element.\r\ntry\r\n    mpos.values(3,3) = NaN;\r\ncatch ENotPos\r\n    disp(ENotPos.message)\r\nend\r\n%%\r\n% Now try a negative number.\r\ntry\r\n    mpos.values(3,3) = -17;\r\ncatch ENotPos\r\n    disp(ENotPos.message)\r\nend\r\n\r\n%%\r\n% I'll try |Inf| next.\r\ntry\r\n    mpos.values(3,3) = Inf;\r\ncatch ENotPos\r\n    disp(ENotPos.message)\r\nend\r\n\r\n%%\r\n% And finally a string.\r\ntry\r\n    mpos.values(3,3) = {'fred'};\r\ncatch ENotPos\r\n    disp(ENotPos.message)\r\nend\r\n\r\n%% Go Negative\r\n% Next let me try to create one of these |PositivelyLoren| objects with\r\n% a matrix that doesn't contain only positive values.\r\nr = randn(3)\r\n%%\r\ntry \r\n    bp = PositivelyLoren(b);\r\ncatch PLE\r\n    disp(PLE.message)\r\nend\r\n\r\n%% The Class\r\n% The class is *very* rudimentary.  You can tell that I didn't go into detail\r\n% in the error message to give better diagnostic information (I could\r\n% have).  And I haven't added lots of useful methods, nor done much to\r\n% contain the data well. Here's the class code.\r\ntype PositivelyLoren\r\n%%\r\n% The class has one property holding the data (|values|).  It's got a\r\n% constructor that allows for the default constructor.  And it has the all\r\n% important |set.values| method that is invoked any time any element in the\r\n% obj is possibly being set.  In there, I make sure, using the\r\n% |validateattributes| function discussed last time, to only allow the data\r\n% to be set if it meets the constraints of finite and positive.\r\n%% How Do You Enforce Rules?\r\n% Have you needed to enforce rules for certain data constructs?  How have\r\n% you done so?  Did you do it in the one-off manor like I did in the last\r\n% post, or have you created your own class?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=219#respond here>.\r\n\r\n\r\n##### SOURCE END ##### 07062a7aec67401dad793674bdce62ad\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      My last post started the discussion about how to ensure data values met certain constraints. The solutions I talked about were ones where\r\n         you checked at certain places during a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/02\/24\/ensuring-positive-values-part-2\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,6,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/219"}],"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=219"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}