<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: I am struggling to program the SWEEP operator to calculate the G1 generalized inverse (AGA = A) in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/576086#M4756</link>
    <description>&lt;P&gt;I'm on vacation, but the SWEEP operator generates an inverse when applied to a SYMMETRIC matrix. Change your example to use a symmetric matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, a good debugging strategy is to use a small matrix that you can print during each iteration of an algorithm. Use a 4x4 or 5x5 symmetric matrix.to develop and debug your program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Jul 2019 09:29:12 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2019-07-24T09:29:12Z</dc:date>
    <item>
      <title>I am struggling to program the SWEEP operator to calculate the G1 generalized inverse (AGA = A)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/575961#M4754</link>
      <description>&lt;P&gt;I am a mathematical statistics masters student at the University of Pretoria, South-Africa.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The SWEEP function in SAS IML works very well to calculate the G1 generalized inverse of a large matrix. I need to calculate the G1 generalized inverse (only satisfying the condition AGA = A, not the Moore-Penrose inverse) of a large matrix using the SWEEP operator. I want to program the sweep operator.&lt;BR /&gt;&lt;BR /&gt;The matrices I need to calculate the G1 generalized inverse of is very large, dimension approximately 3000 x 3000.&lt;BR /&gt;&lt;BR /&gt;I have tried to program the sweep operator to calculate the G1 generalized inverse using the following article as a reference&lt;BR /&gt;"A Tutorial on the SWEEP Operator" Author: James H. Goodnight.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My program is not generating the correct G1 generalized inverse.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also want to make the program efficient.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Can you please guide me and give me advice on how to program the sweep operator?&lt;BR /&gt;&lt;BR /&gt;Is it wise to use a loop in the program?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;ginv(A) = SWEEP(A,1:nrow(A))&lt;BR /&gt;The sweep operator applied to all the rows/columns of the matrix = G1 generalized inverse&lt;BR /&gt;Is there some trick that can help me make my program more efficient? Since I am working with very large matrices.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
/*SWEEP operator to calculate G1 - first attempt*/
start sweep_ginv(A);
	n = nrow(A);
	do k = 1 to n;
		B = J(n,n,0);
		print A;
		p = A[k,k];
		B[setdif(1:n,k),k] = -A[setdif(1:n,k),k]/p;
		B[k,setdif(1:n,k)] = A[k,setdif(1:n,k)]/p;
		B[setdif(1:n,k),setdif(1:n,k)] = A[setdif(1:n,k),setdif(1:n,k)]-(1/p)*A[setdif(1:n,k),k]*A[k,setdif(1:n,k)];
		B[k,k] = 1/p;
		print B;
		A = B;
		print A;
	end;
	return(B);
finish sweep_ginv;

***********************************************************************;
/*Second attempt*/
start sweep_ginv2(F);
	n = nrow(F);
	range = 1:n;
	do k = 1 to n;
		B = J(n,n,0);
		p = F[k,k];
		B[loc(range ^= k),k] = -F[loc(range ^= k),k]/p;
		B[k,loc(range ^= k)] = F[k,loc(range ^= k)]/p;
		B[loc(range ^= k),loc(range ^= k)] = F[loc(range ^= k),loc(range ^= k)]-(1/p)*F[loc(range ^= k),k]*F[k,loc(range ^= k)];
		B[k,k] = 1/p;
		F = B;
	end;
return(B);
finish sweep_ginv2;

******************************************************************;
/*Test for small matrices first*/
seed = 1234;
n = 40;
c = J(n, n, seed); 
A = normal(c);

*******************************************************************;
/*Test for generalized inverse
the sum should be zero*/
T = sweep_ginv2(A);
sum = sum(A*T*A - A);
print sum;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Jul 2019 19:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/575961#M4754</guid>
      <dc:creator>Esta_Stoop</dc:creator>
      <dc:date>2019-07-23T19:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: I am struggling to program the SWEEP operator to calculate the G1 generalized inverse (AGA = A)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/575967#M4755</link>
      <description>&lt;P&gt;See if this document from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;helps:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/11/21/generalized-inverses-for-matrices.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2018/11/21/generalized-inverses-for-matrices.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, when you are trying to do some common mathematical algorithm in PROC IML, Rick has already explained how to do it, so his blog is a good place to start looking for help.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2019 20:06:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/575967#M4755</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-07-23T20:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: I am struggling to program the SWEEP operator to calculate the G1 generalized inverse (AGA = A)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/576086#M4756</link>
      <description>&lt;P&gt;I'm on vacation, but the SWEEP operator generates an inverse when applied to a SYMMETRIC matrix. Change your example to use a symmetric matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, a good debugging strategy is to use a small matrix that you can print during each iteration of an algorithm. Use a 4x4 or 5x5 symmetric matrix.to develop and debug your program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2019 09:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/I-am-struggling-to-program-the-SWEEP-operator-to-calculate-the/m-p/576086#M4756</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-07-24T09:29:12Z</dc:date>
    </item>
  </channel>
</rss>

