<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487</link>
    <title>MATLAB Central Newsreader - Largest Common Factor</title>
    <description>Feed for thread: Largest Common Factor</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 30 Apr 2008 00:12:05 -0400</pubDate>
      <title>Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429484</link>
      <author>Greg Heath</author>
      <description>What is the best way to find the largest common&lt;br&gt;
factor of an array? For example, 4.1 is the largest  common factor of&lt;br&gt;
[12.3 20.5 32.8]].&lt;br&gt;
I've looped over whole fractions of 12.3 to&lt;br&gt;
get an integer array but feel there must be a better way.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Greg&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 01:50:02 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429490</link>
      <author>Roger Stafford</author>
      <description>Greg Heath &amp;lt;heath@alumni.brown.edu&amp;gt; wrote in message &lt;br&gt;
&amp;lt;7ebde538-62ed-4bba-&lt;br&gt;
aed4-15843490c573@24g2000hsh.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; What is the best way to find the largest common&lt;br&gt;
&amp;gt; factor of an array? For example, 4.1 is the largest  common factor of&lt;br&gt;
&amp;gt; [12.3 20.5 32.8]].&lt;br&gt;
&amp;gt; I've looped over whole fractions of 12.3 to&lt;br&gt;
&amp;gt; get an integer array but feel there must be a better way.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Greg&lt;br&gt;
---------&lt;br&gt;
&amp;nbsp;&amp;nbsp;Euclid's algorithm would still work for non-integer elements like 12.3, but it &lt;br&gt;
would have to have a tolerance that allows for round-off errors.  For example, &lt;br&gt;
the binary floating point value matlab uses to approximate 12.3 is not an &lt;br&gt;
exact integral multiple of the number with which it approximates 4.1.  I don't &lt;br&gt;
know whether the current version of 'gcd' makes such an allowance or &lt;br&gt;
whether it even permits non-integers as input arguments.  (My own version &lt;br&gt;
does not.)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Using Euclid's algorithm, the numbers 12.3 and 20.5 would go:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;rem(20.5,12.3) = 8.2&lt;br&gt;
&amp;nbsp;rem(12.3, 8.2) = 4.1&lt;br&gt;
&amp;nbsp;rem( 8.2, 4.1) = 0.0&lt;br&gt;
&lt;br&gt;
except that this last one wouldn't be exactly zero but only within some &lt;br&gt;
tolerated distance from zero.  If you started with, say, pi and sqrt(2), this &lt;br&gt;
procedure would not stop until it had dropped far down into numbers in the &lt;br&gt;
tolerated noise level.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I can think of no better way of handling more than two elements than &lt;br&gt;
repeating the above operation using the largest common factor found up to a &lt;br&gt;
point in an array, together with the array's next element.  Short of a matlab &lt;br&gt;
function designed for this, it sounds like using a common garden variety for-&lt;br&gt;
loop to accomplish this feat is your best bet.  This algorithm does not appear &lt;br&gt;
to lend itself to any kind of vectorizaton that I can think of.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 05:41:01 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429517</link>
      <author>Yumnam Kirani Singh</author>
      <description>Largest common factor is defined only for integers not for floating point numbers. See hep on gcd.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 06:58:02 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429527</link>
      <author>Bruno Luong</author>
      <description>"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt;&lt;br&gt;
wrote in message &amp;lt;fv8j8a$2j8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   I can think of no better way of handling more than two&lt;br&gt;
elements than &lt;br&gt;
&amp;gt; repeating the above operation using the largest common&lt;br&gt;
factor found up to a &lt;br&gt;
&amp;gt; point in an array, together with the array's next element.&lt;br&gt;
&lt;br&gt;
I also think euclide's algorithm is a great idea. But we&lt;br&gt;
need to define how to apply on the array. What about&lt;br&gt;
&lt;br&gt;
1) find the minimum element "p" (absolute value) of the array&lt;br&gt;
2) Replace all other elements by the remaining with "p".&lt;br&gt;
3) if all the replacements are "zero" (within a tolerance)&lt;br&gt;
then stop&lt;br&gt;
4) Otherwise loop on 1)&lt;br&gt;
&lt;br&gt;
When break the loop, p is the answer.&lt;br&gt;
&lt;br&gt;
Bruno&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 10:42:02 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429554</link>
      <author>Bruno Luong</author>
      <description>"Bruno Luong" &amp;lt;b.luong@fogale.fr&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fv959q$34q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; "Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt;&lt;br&gt;
&amp;gt; wrote in message &amp;lt;fv8j8a$2j8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;   I can think of no better way of handling more than two&lt;br&gt;
&amp;gt; elements than &lt;br&gt;
&amp;gt; &amp;gt; repeating the above operation using the largest common&lt;br&gt;
&amp;gt; factor found up to a &lt;br&gt;
&amp;gt; &amp;gt; point in an array, together with the array's next element.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I also think euclide's algorithm is a great idea. But we&lt;br&gt;
&amp;gt; need to define how to apply on the array. What about&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1) find the minimum element "p" (absolute value) of the array&lt;br&gt;
&amp;gt; 2) Replace all other elements by the remaining with "p".&lt;br&gt;
&amp;gt; 3) if all the replacements are "zero" (within a tolerance)&lt;br&gt;
&amp;gt; then stop&lt;br&gt;
&amp;gt; 4) Otherwise loop on 1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; When break the loop, p is the answer.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Here is a code of the above idea:&lt;br&gt;
&lt;br&gt;
asize=100;&lt;br&gt;
maxa=10;&lt;br&gt;
myeps=1e-10; % small number&lt;br&gt;
% generate asize array, multiple of 4.1&lt;br&gt;
a=4.1*(1+round(rand(1,asize)*(maxa-1)));&lt;br&gt;
&lt;br&gt;
% The engine!&lt;br&gt;
&lt;br&gt;
aworking=a;&lt;br&gt;
while 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[p imin]=min(aworking);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;irest=[1:imin-1 imin+1:length(aworking)];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arest=mod(aworking(irest),p);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arest(abs(arest)&amp;lt;myeps)=nan; % lock&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if any(abs(arest))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;aworking(irest)=arest;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
disp(p);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 11:12:03 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429558</link>
      <author>Roger Stafford</author>
      <description>"Bruno Luong" &amp;lt;b.luong@fogale.fr&amp;gt; wrote in message &amp;lt;fv959q$34q&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I also think euclide's algorithm is a great idea. But we&lt;br&gt;
&amp;gt; need to define how to apply on the array. What about&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1) find the minimum element "p" (absolute value) of the array&lt;br&gt;
&amp;gt; 2) Replace all other elements by the remaining with "p".&lt;br&gt;
&amp;gt; 3) if all the replacements are "zero" (within a tolerance)&lt;br&gt;
&amp;gt; then stop&lt;br&gt;
&amp;gt; 4) Otherwise loop on 1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; When break the loop, p is the answer.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
-----------&lt;br&gt;
&amp;nbsp;&amp;nbsp;I would envision implementing Euclid's algorithm for arrays of non-integers &lt;br&gt;
with something like the following.  Let x be a vector of values, not necessarily &lt;br&gt;
integers and not necessarily of like sign, for which we wish to find the largest &lt;br&gt;
common factor, f.  That is, we want the largest magnitude, f, such that all &lt;br&gt;
elements of x are integer multiples of f (ignoring round-off errors.)  For this &lt;br&gt;
to work properly, the vector x should not contain any zero or nearly zero &lt;br&gt;
values - the multiples should be non-zero multiples.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;tol = 1e-8;&lt;br&gt;
&amp;nbsp;f = x(1);&lt;br&gt;
&amp;nbsp;for k = 2:length(x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;r = f;&lt;br&gt;
&amp;nbsp;&amp;nbsp;f = x(k);&lt;br&gt;
&amp;nbsp;&amp;nbsp;while abs(r) &amp;gt; tol&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;t = f;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;f = r;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;r = rem(t,f);&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&amp;nbsp;f = abs(f); % This is the largest common factor of elements in x&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The value for 'tol' here is not necessarily optimum.  The best choice would &lt;br&gt;
depend on the magnitudes of elements in x and on the length of x, but I &lt;br&gt;
haven't worked out a good criterion as yet.  It does have to be chosen &lt;br&gt;
carefully.  If it is too small, it will miss cases where a common factor exists &lt;br&gt;
but gets overlooked because of accumulating round-off errors.  If it is too &lt;br&gt;
large, it will allow false, overly-large common factors to be admitted.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 16:01:39 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429617</link>
      <author>Greg Heath</author>
      <description>On Apr 30, 1:41=A0am, Yumnam Kirani Singh &amp;lt;kirani.si...@gmail.com&amp;gt;&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; Largest common factor is defined only for integers not for floating point =&lt;br&gt;
numbers. See hep on gcd.&lt;br&gt;
&lt;br&gt;
For my purposes I can truncate FP numbers to any&lt;br&gt;
practical precision then multiply by the appropriate power&lt;br&gt;
of 10 to get integers.&lt;br&gt;
&lt;br&gt;
Thanks.&lt;br&gt;
&lt;br&gt;
Greg&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 16:04:56 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429619</link>
      <author>Greg Heath</author>
      <description>On Apr 30, 7:12=A0am, "Roger Stafford"&lt;br&gt;
&amp;lt;ellieandrogerxy...@mindspring.com.invalid&amp;gt; wrote:&lt;br&gt;
&amp;gt; "Bruno Luong" &amp;lt;b.lu...@fogale.fr&amp;gt; wrote in message &amp;lt;fv959q$34q&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; $...@fred.mathworks.com&amp;gt;...&amp;gt; I also think euclide's algorithm is a great i=&lt;br&gt;
dea. But we&lt;br&gt;
&amp;gt; &amp;gt; need to define how to apply on the array. What about&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; 1) find the minimum element "p" (absolute value) of the array&lt;br&gt;
&amp;gt; &amp;gt; 2) Replace all other elements by the remaining with "p".&lt;br&gt;
&amp;gt; &amp;gt; 3) if all the replacements are "zero" (within a tolerance)&lt;br&gt;
&amp;gt; &amp;gt; then stop&lt;br&gt;
&amp;gt; &amp;gt; 4) Otherwise loop on 1)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; When break the loop, p is the answer.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Bruno&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; -----------&lt;br&gt;
&amp;gt; =A0 I would envision implementing Euclid's algorithm for arrays of non-int=&lt;br&gt;
egers&lt;br&gt;
&amp;gt; with something like the following. =A0Let x be a vector of values, not nec=&lt;br&gt;
essarily&lt;br&gt;
&amp;gt; integers and not necessarily of like sign, for which we wish to find the l=&lt;br&gt;
argest&lt;br&gt;
&amp;gt; common factor, f. =A0That is, we want the largest magnitude, f, such that =&lt;br&gt;
all&lt;br&gt;
&amp;gt; elements of x are integer multiples of f (ignoring round-off errors.) =A0F=&lt;br&gt;
or this&lt;br&gt;
&amp;gt; to work properly, the vector x should not contain any zero or nearly zero&lt;br&gt;
&amp;gt; values - the multiples should be non-zero multiples.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; =A0tol =3D 1e-8;&lt;br&gt;
&amp;gt; =A0f =3D x(1);&lt;br&gt;
&amp;gt; =A0for k =3D 2:length(x)&lt;br&gt;
&amp;gt; =A0 r =3D f;&lt;br&gt;
&amp;gt; =A0 f =3D x(k);&lt;br&gt;
&amp;gt; =A0 while abs(r) &amp;gt; tol&lt;br&gt;
&amp;gt; =A0 =A0t =3D f;&lt;br&gt;
&amp;gt; =A0 =A0f =3D r;&lt;br&gt;
&amp;gt; =A0 =A0r =3D rem(t,f);&lt;br&gt;
&amp;gt; =A0 end&lt;br&gt;
&amp;gt; =A0end&lt;br&gt;
&amp;gt; =A0f =3D abs(f); % This is the largest common factor of elements in x&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; =A0 The value for 'tol' here is not necessarily optimum. =A0The best choic=&lt;br&gt;
e would&lt;br&gt;
&amp;gt; depend on the magnitudes of elements in x and on the length of x, but I&lt;br&gt;
&amp;gt; haven't worked out a good criterion as yet. =A0It does have to be chosen&lt;br&gt;
&amp;gt; carefully. =A0If it is too small, it will miss cases where a common factor=&lt;br&gt;
&amp;nbsp;exists&lt;br&gt;
&amp;gt; but gets overlooked because of accumulating round-off errors. =A0If it is =&lt;br&gt;
too&lt;br&gt;
&amp;gt; large, it will allow false, overly-large common factors to be admitted.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
&lt;br&gt;
Roger and Bruno,&lt;br&gt;
&lt;br&gt;
Thanks. The application will be to overlay a uniform grid on a&lt;br&gt;
nonuniform grid for the purposes of using the fft instead of a dft.&lt;br&gt;
&lt;br&gt;
Greg&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 30 Apr 2008 18:04:26 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429641</link>
      <author>Greg Heath</author>
      <description>On Apr 30, 12:01 pm, Greg Heath &amp;lt;he...@alumni.brown.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; On Apr 30, 1:41 am, Yumnam Kirani Singh &amp;lt;kirani.si...@gmail.com&amp;gt;&lt;br&gt;
&amp;gt; wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Largest common factor is defined only for integers&lt;br&gt;
&amp;gt;&amp;gt; not for floating point numbers. See hep on gcd.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; For my purposes I can truncate FP numbers to any&lt;br&gt;
&amp;gt; practical precision then multiply by the appropriate&lt;br&gt;
&amp;gt; power of 10 to get integers.&lt;br&gt;
&lt;br&gt;
Example:&lt;br&gt;
&lt;br&gt;
a = 2*pi*sort(rand(128,1));  % realistic data&lt;br&gt;
a = [12.3 20.5 32.8]             % demo data&lt;br&gt;
tol = 4;&lt;br&gt;
&lt;br&gt;
% [g h] = GCFgh(a,tol)&lt;br&gt;
% function [g h] = GCFgh(a,tol)&lt;br&gt;
&lt;br&gt;
mult = 10^tol;&lt;br&gt;
b = round(mult*a);&lt;br&gt;
c = b(1)*ones(size(a));&lt;br&gt;
d = gcd(b,c);&lt;br&gt;
g = min(d)/mult&lt;br&gt;
h = a/g&lt;br&gt;
&lt;br&gt;
a =         12.3         20.5         32.8&lt;br&gt;
g =          4.1&lt;br&gt;
h =            3            5            8&lt;br&gt;
&lt;br&gt;
Greg&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 01 May 2008 01:31:17 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429702</link>
      <author>Greg Heath</author>
      <description>On Apr 30, 2:04=A0pm, Greg Heath &amp;lt;he...@alumni.brown.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; On Apr 30, 12:01 pm, Greg Heath &amp;lt;he...@alumni.brown.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; On Apr 30, 1:41 am, Yumnam Kirani Singh &amp;lt;kirani.si...@gmail.com&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; Largest common factor is defined only for integers&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; not for floating point numbers. See hep on gcd.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; For my purposes I can truncate FP numbers to any&lt;br&gt;
&amp;gt; &amp;gt; practical precision then multiply by the appropriate&lt;br&gt;
&amp;gt; &amp;gt; power of 10 to get integers.&lt;br&gt;
&lt;br&gt;
a is positive and monotonic increasing&lt;br&gt;
&lt;br&gt;
function [g, h] =3D GCFgh(a,tol);&lt;br&gt;
b =3D round(a/tol);&lt;br&gt;
c =3D b(1)*ones(size(a));&lt;br&gt;
d =3D gcd(b,c);&lt;br&gt;
g =3D tol*min(d);&lt;br&gt;
h =3D round(a/g);&lt;br&gt;
return&lt;br&gt;
&lt;br&gt;
There seems to be something in gcd that is missing in&lt;br&gt;
Bruno and Rogers code ... or does one have to choose&lt;br&gt;
a more appropriate value for tol? I tried a few, but half of&lt;br&gt;
the time there were errors of O(1e-16):&lt;br&gt;
&lt;br&gt;
close all, clear, clc, disp(datestr(now))&lt;br&gt;
&lt;br&gt;
tol =3D 1e-10;&lt;br&gt;
format long e&lt;br&gt;
&lt;br&gt;
z0 =3D randperm(128)';&lt;br&gt;
h0 =3D sort(z0(1:32));&lt;br&gt;
&lt;br&gt;
% g00 =3D rand&lt;br&gt;
g00 =3D 0.4567890123456789&lt;br&gt;
g0  =3D tol*round(g00/tol)&lt;br&gt;
dg  =3D g0-g00&lt;br&gt;
a   =3D g0*h0;&lt;br&gt;
&lt;br&gt;
% figure,hold on&lt;br&gt;
% plot(g0*(1:128))&lt;br&gt;
% plot(h0,a,'r.')&lt;br&gt;
&lt;br&gt;
[g h]    =3D GCFgh(a,tol)&lt;br&gt;
err(1,1) =3D maxabs(g-g0);&lt;br&gt;
err(2,1) =3D maxabs(h-h0);&lt;br&gt;
err(3,1) =3D maxabs(a-g*h);&lt;br&gt;
&lt;br&gt;
[g h]    =3D GCFbl(a,tol)&lt;br&gt;
err(1,2) =3D maxabs(g-g0);&lt;br&gt;
err(2,2) =3D maxabs(h-h0);&lt;br&gt;
err(3,2) =3D maxabs(a-g*h);&lt;br&gt;
&lt;br&gt;
[g h]    =3D GCFrs(a,tol)&lt;br&gt;
err(1,3) =3D maxabs(g-g0);&lt;br&gt;
err(2,3) =3D maxabs(h-h0);&lt;br&gt;
err(3,3) =3D maxabs(a-g*h);&lt;br&gt;
&lt;br&gt;
disp(err)&lt;br&gt;
&lt;br&gt;
%   0    1.831867990631508e-015    7.216449660063518e-016&lt;br&gt;
%   0                         0                         0&lt;br&gt;
%   0    2.344791028008331e-013    9.237055564881302e-014&lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
Greg&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 01 May 2008 09:10:06 -0400</pubDate>
      <title>Re: Largest Common Factor</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168487#429738</link>
      <author>Bruno Luong</author>
      <description>Greg Heath &amp;lt;heath@alumni.brown.edu&amp;gt; wrote in message&lt;br&gt;
&amp;lt;48d14e27-7130-48a7-9c9c-ddf9058363dc@f63g2000hsf.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There seems to be something in gcd that is missing in&lt;br&gt;
&amp;gt; Bruno and Rogers code ... or does one have to choose&lt;br&gt;
&amp;gt; a more appropriate value for tol? I tried a few, but half of&lt;br&gt;
&amp;gt; the time there were errors of O(1e-16):&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Hi Greg,&lt;br&gt;
&lt;br&gt;
No I d'ont believe the accuracy issue lies on tol, but&lt;br&gt;
rather on roundoff of successive division (in mod and rem).&lt;br&gt;
&lt;br&gt;
One quick fix it adjusting the common factor "g" at the end&lt;br&gt;
of the loop as following:&lt;br&gt;
&lt;br&gt;
k=round(a/g);&lt;br&gt;
g=mean(a./k);&lt;br&gt;
&lt;br&gt;
%%%%%%%%&lt;br&gt;
%% Here is the full test for my algo&lt;br&gt;
&lt;br&gt;
clear&lt;br&gt;
tol =1e-10;&lt;br&gt;
format long e&lt;br&gt;
&lt;br&gt;
z0 =randperm(128)';&lt;br&gt;
h0 =sort(z0(1:32));&lt;br&gt;
&lt;br&gt;
g00 = 0.4567890123456789;&lt;br&gt;
g0 = tol*round(g00/tol);&lt;br&gt;
dg = g0-g00;&lt;br&gt;
a = g0*h0;&lt;br&gt;
&lt;br&gt;
% The engine!&lt;br&gt;
aworking=a;&lt;br&gt;
while 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[g imin]=min(aworking);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;irest=[1:imin-1 imin+1:length(aworking)];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arest=mod(aworking(irest),g);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arest(abs(arest)&amp;lt;tol)=nan; % lock&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if any(abs(arest))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;aworking(irest)=arest;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
k=round(a/g);&lt;br&gt;
g=mean(a./k);&lt;br&gt;
disp(g)&lt;br&gt;
&lt;br&gt;
% Bruno&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
