<?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 Vectorizing in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793300#M5765</link>
    <description>&lt;P&gt;Hello to everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have asked so many questions about this subject, I am trying to code an algorithm for the Particle Swarm Optimization. I have asked in this forum some days ago and Rick_SAS told me that it would be interesting to vectorize my code. So I have read some articles about the subject and I have taken a sheet of paper and a pen, for trying to achieve the result. I think that I don't have the logic behind and it's a little bit frustrating. So I don't know if someone can show me a little example with this code. In order to try to see the logic with my own code. Here you have a little fragment of my code, I have tried so many things and I failed. Do not hesitate to ask me if you need some additional explanation. Thank you all.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;xmin=-100;
xmax=100;
/***parameters and variables***/
n=20;						/*number of particules*/
p=2;
maxt=1000;					/*maximum iteration*/
maxrun=100;					/*trials of the algorithm*/
X=j(n,p+1,0);				/*initialization of the particle's matrix*/
lb=j(1,p,xmin);
ub=j(1,p,xmax);

do run=1 to maxrun; /*this line is not important in this example*/
	nb_t[1,run]=run;
					/***initialization of the pso***/
	do i=1 to n;
		do j=1 to p;
	/*sample in a uniform distribution to initialize the particles position*/
			call randgen(u,'uniform');
			X[i,j]=lb[j]+u*(ub[j]-lb[j]);
		end;
	/*to fill the third column of X (which corresponds to the output of the function), we evaluate the fitting for each particule*/
		X[i,p+1]=f(X[i,]);
	end;
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 29 Jan 2022 18:17:30 GMT</pubDate>
    <dc:creator>CerditoSalvaje</dc:creator>
    <dc:date>2022-01-29T18:17:30Z</dc:date>
    <item>
      <title>Vectorizing</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793300#M5765</link>
      <description>&lt;P&gt;Hello to everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have asked so many questions about this subject, I am trying to code an algorithm for the Particle Swarm Optimization. I have asked in this forum some days ago and Rick_SAS told me that it would be interesting to vectorize my code. So I have read some articles about the subject and I have taken a sheet of paper and a pen, for trying to achieve the result. I think that I don't have the logic behind and it's a little bit frustrating. So I don't know if someone can show me a little example with this code. In order to try to see the logic with my own code. Here you have a little fragment of my code, I have tried so many things and I failed. Do not hesitate to ask me if you need some additional explanation. Thank you all.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;xmin=-100;
xmax=100;
/***parameters and variables***/
n=20;						/*number of particules*/
p=2;
maxt=1000;					/*maximum iteration*/
maxrun=100;					/*trials of the algorithm*/
X=j(n,p+1,0);				/*initialization of the particle's matrix*/
lb=j(1,p,xmin);
ub=j(1,p,xmax);

do run=1 to maxrun; /*this line is not important in this example*/
	nb_t[1,run]=run;
					/***initialization of the pso***/
	do i=1 to n;
		do j=1 to p;
	/*sample in a uniform distribution to initialize the particles position*/
			call randgen(u,'uniform');
			X[i,j]=lb[j]+u*(ub[j]-lb[j]);
		end;
	/*to fill the third column of X (which corresponds to the output of the function), we evaluate the fitting for each particule*/
		X[i,p+1]=f(X[i,]);
	end;
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 18:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793300#M5765</guid>
      <dc:creator>CerditoSalvaje</dc:creator>
      <dc:date>2022-01-29T18:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorizing</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793307#M5766</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As this is SAS/IML code, I have moved your post to the&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS/IML Software and Matrix Computations&lt;/P&gt;
&lt;P&gt;board.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No absolute need to do it in SAS/IML by the way.&lt;BR /&gt;See here :&lt;BR /&gt;Particle Swarm Optimization in SAS&lt;BR /&gt;Gregory Watson, UCLA Department of Biostatistics, Los Angeles, CA&lt;BR /&gt;&lt;A href="https://www.lexjansen.com/wuss/2014/63_Final_Paper_PDF.pdf" target="_blank"&gt;https://www.lexjansen.com/wuss/2014/63_Final_Paper_PDF.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 19:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793307#M5766</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-01-29T19:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorizing</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793316#M5767</link>
      <description>&lt;P&gt;I applaud you for taking on this challenge. It might be frustrating in the short term, but it will pay off in the long term. See these tips:&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2013/11/18/vectorizing-the-construction-of-a-structured-matrix.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/11/18/vectorizing-the-construction-of-a-structured-matrix.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Assuming that the objective function involves fractional powers (as in your last post), I have adjusted xmin=0.&lt;/P&gt;
&lt;P&gt;2. I have also vectorized the objective function.&lt;/P&gt;
&lt;P&gt;3. If you are not familiar with the '#' multiplication operator, the operation v#Y is shorthand notation for multiplying the i_th column of the matrix Y by i_th element of the row vector, v. See &lt;A href="https://blogs.sas.com/content/iml/2010/12/06/shorthand-notation-for-row-and-column-operations.html" target="_self"&gt;"Shorthand notation for row and column operations."&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
xmin=0;     /* min has to be &amp;gt;= 0 or else x^(1/4) and x^(3/4) are undefined */
xmax=100;
/***parameters and variables***/
n=20;			   			/*number of particles*/
p=2;
maxt=1000;					/*maximum iteration*/
maxrun=1;					/*trials of the algorithm*/
X=j(n,p+1,0);				/*initialization of the particle's matrix*/
lb=j(1,p,xmin);
ub=j(1,p,xmax);

/* vectorization of f: R^2 --&amp;gt; R */
start f(x);
   con=100*x[,1]+200*x[,2]-1500;
   fc = 3*(x[,1]##0.25) # (x[,2]##0.75);
   pen=10**9;
   idx = loc(con &amp;gt; 0);
   if ncol(type)&amp;gt;0 then 
      fc[idx] = pen*con[idx];
   return(fc);
finish f;

call randseed(1234);
Y = j(n,P);         /* allocate for random values */
do run=1 to maxrun; /*this line is not important in this example*/
   /***initialization of the pso***/
   call randgen(Y, 'uniform');
	/*sample in a uniform distribution to initialize the particles position*/
   X[,1:p] = lb + (ub-lb)#Y;
	/*to fill the third column of X (which corresponds to the output of the function), we evaluate the fitting for each particule*/
	X[,p+1]= f(X);
end;

print X[c={'x1' 'x2' 'F(x1,x2)'}];
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I understand that vectorized operations can be hard to learn, so write back if you need anything clarified.&amp;nbsp; When you vectorize, the resulting code is very compact. It is like reading poetry: there is a lot of information in each line.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 21:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793316#M5767</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-01-29T21:12:57Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorizing</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793318#M5768</link>
      <description>Thank you so much. I will start right now. If I get it for the whole code I'll tell you. Thanks.</description>
      <pubDate>Sat, 29 Jan 2022 22:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorizing/m-p/793318#M5768</guid>
      <dc:creator>CerditoSalvaje</dc:creator>
      <dc:date>2022-01-29T22:55:48Z</dc:date>
    </item>
  </channel>
</rss>

