Diff of /@mp/mp.m [e7306a] .. [471bfe] Maximize Restore

  Switch to side-by-side view

--- a/@mp/mp.m
+++ b/@mp/mp.m
@@ -1,33 +1,34 @@
 function out = mp(x, y)
-%MP multiple  precision class constructor.
-%   p = mp(x, y) creates a mp object from the matrices x and y,
-%   where x contains the double to be converted into an mp object
-%         y contains the precision
-% Special calls to mp include:
-%   mp('pi', precision) => returns pi to precision (precision is optional)
+  %#MP multiple  precision class constructor.
+  %#   p = mp(x, y) creates a mp object from the matrices x and y,
+  %#   where x contains the double to be converted into an mp object
+  %#         y contains the precision
+  %# Special calls to mp include:
+  %#   mp('pi', precision) => returns pi to precision (precision is optional)
 
-maxDoublePrec = 16; %digits
+  maxDoublePrec = 16; %digits
+  
+  if (0 == nargin) %# empty constructor, returns the basic structure
+    out = struct ('rval', '', 'ival', '', 'precision', int32 (0));
+    out = class (out, 'mp');
+    return
+  end
 
-if (0 == nargin)
- out.rval = '';
- out.ival = '';
- out.precision = int32(0);
- out = class (out, 'mp');
- return
-end
+  if (isa (x, 'mp'))
+    if (1 == nargin) 	%# no op
+      out = x;
+    else			%# adapt precision
+      for ii = (1:numel (x))
+	out(ii) = __mp__ ({x(ii).rval}, y(min (numel (y), ii)), true);
+	if (~isreal (x(ii)))
+	  __mp__ ({x(ii).ival}, y(min (numel (y), ii)), true);
+	  out(ii) = complex (out(ii), ans)
+	end
+      end
+    end % if nargin==2
+    return
+  end
 
-if (isa (x, 'mp'))
-  if (1 == nargin) 	%# no op
-    out = x;
-  else			%# adapt precision
-    for ii = (1:numel (x))
-      out(ii) = __mp__ (x(ii).rval, y(min (numel (y), ii)), true);
-      if (~isreal (x(ii)))
-	out(ii) = out(ii) + __mp__ (x(ii).ival, y(min (numel (y), ii)), true)*i;
-      end
-    end
-  end % if nargin==2
-else
   if (isa (x, 'struct'))
     %# bless this struct into a mp object
     out = mp ();
@@ -41,73 +42,70 @@
       error ('First argument of mp is a struct with wrong field names');
     end
     out = class (x, 'mp');
+    return
+  end
+  
+  %# cast "anything" to a mp
+  mp_defaults
+  
+  if (2 == nargin)
+    precision = int32 (max (y(:)));
   else
-    %# cast "anything" to a mp
-    mp_defaults
+    precision = default_precision;
+  end % if nargin==2
   
-    if (2 == nargin)
-      precision = int32 (max (y(:)));
-    else
-      precision = default_precision;
-    end % if nargin==2
-    
-    if (isnumeric (x))
-      %# case where NaN and Inf are encoded accordingly.
-      out_rval = cell (size (x));
-      out_ival = cell (size (x));
-      for ii = (1:numel (x))
-	if (isnan (real (x(ii))))
-	  out_rval{ii} = mpfr_construct_nan (double (precision));
-	elseif (isinf (real (x(ii))))
-	  out_rval{ii} = mpfr_construct_inf (double (precision), double (sign (real (x(ii)))));
+  if (isnumeric (x))
+    %# case where NaN and Inf are encoded accordingly.
+    [out_rval, out_ival] = deal (cell (size (x)));
+    for ii = (1:numel (x))
+      if (isnan (real (x(ii))))
+	out_rval{ii} = mpfr_construct_nan (double (precision));
+      elseif (isinf (real (x(ii))))
+	out_rval{ii} = mpfr_construct_inf (double (precision), ...
+					   double (sign (real (x(ii)))));
+      else
+	[str, exponent] = ...
+        mpfr_construct_dd (real (double (x(ii))), double (precision));
+	% throw away anything past maxDoublePrec, set to 0's
+	if (length (str) > maxDoublePrec)
+	  str(maxDoublePrec+1:end) = '0';
+	end
+	out_rval{ii} = mpExpForm (mpAddDecimal (str), exponent);
+      end
+      if (~isreal (x(ii)))
+	x(ii) = imag (x(ii));
+	if (isnan (x(ii)))
+	  out_ival{ii} = mpfr_construct_nan (double (precision));
+	elseif (isinf (x(ii)))
+	  out_ival{ii} = mpfr_construct_inf (double (precision), ...
+					     double (sign (x(ii))));
 	else
 	  [str, exponent] = ...
-          mpfr_construct_dd (real (double (x(ii))), double (precision));
+	  mpfr_construct_dd (double (x(ii)), double (precision));
 	  % throw away anything past maxDoublePrec, set to 0's
 	  if (length (str) > maxDoublePrec)
 	    str(maxDoublePrec+1:end) = '0';
 	  end
-	  out_rval{ii} = mpExpForm (mpAddDecimal (str), exponent);
-	end
-	if (~isreal (x(ii)))
-	  x(ii) = imag (x(ii));
-	  if (isnan (x(ii)))
-	    out_ival{ii} = mpfr_construct_nan (double (precision));
-	  elseif (isinf (x(ii)))
-	    out_ival{ii} = mpfr_construct_inf (double (precision), double (sign (x(ii))));
-	  else
-	    [str, exponent] = ...
-	    mpfr_construct_dd (double (x(ii)), double (precision));
-	    % throw away anything past maxDoublePrec, set to 0's
-	    if (length (str) > maxDoublePrec)
-	      str(maxDoublePrec+1:end) = '0';
-	    end
-	    out_ival{ii} = mpExpForm (mpAddDecimal (str), exponent);
-	  end	
-	end
-      end % for ii=1:size(x,
-      out = class (struct ('rval', out_rval,...
-			   'ival', out_ival,...
-			   'precision', precision), 'mp');
-    elseif (isa (x, 'cell'))
-      out = __mp__ (x, precision, false); 
-    elseif (isa (x, 'char'))
-      warning ('mp: this call is not safe with respect to NaN and Inf');
-      keyboard
-      out_rval = cell (1);
-      out_ival = cell (1);
-      x = lower(x);
-      if (any (strfind (lower (x), 'pi')))
-	out_rval = mpfr_pi (precision);
-      %%%   [str,exponent]=mpfr_pi(precision);
-      %%%   out_rval=mpExpForm(mpAddDecimal(str),exponent);
-      else
-	[str, exponent]= mpfr_construct_cd (x, double (precision));
-	out_rval{1,1} = mpExpForm (mpAddDecimal (str), exponent);
+	  out_ival{ii} = mpExpForm (mpAddDecimal (str), exponent);
+	end	
       end
-    end
+    end % for ii=1:size(x,
     out = class (struct ('rval', out_rval,...
 			 'ival', out_ival,...
 			 'precision', precision), 'mp');
+    return
   end
+
+  if (isa (x, 'cell'))
+    out = __mp__ (x, precision, false); 
+    return
+  end
+  
+  if (isa (x, 'char'))
+    out = __mp__(cellstr (x), precision, false);
+    return
+  end
+
+  disp('mp : unhandled case ???');
+
 end