<?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 Outer product capability for dyadic functions in IML in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Outer-product-capability-for-dyadic-functions-in-IML/m-p/887369#M6009</link>
    <description>&lt;P&gt;I have written an IML module to compute a table created by the application of a dyadic function ( '+', '#', '/', etc.) applied to two numeric vectors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start o_p( dyadic_fcn, x, y ) ;
   /* purpose: compute outer product of vectors x, y by applying dyadic function
    *
    * parameters:
    *    dyadic_fcn ::= quoted dyadic function, e.g., + - = ^= # ## &amp;lt; &amp;gt; , &amp;amp;cetera
    *    x          ::= numeric scalar or vector
    *    y          ::= numeric scalar or vector
    *
    * returns outer product of column vector x &amp;lt;dyadic function&amp;gt; row vector y
    *
    *  example:
    *    a = 1 : 4 ; b = 1 : 5 ;
    *
    *    c = op( '/', a, b ) ;
    */

   tmp_x = colvec( x ) ; n_row_x = nrow( tmp_x ) ; 
   tmp_y = rowvec( y ) ; n_col_y = ncol( tmp_y ) ;

   x_mat = repeat( tmp_x, 1, n_col_y ) ;&lt;BR /&gt;   y_mat = repeat( tmp_y, n_row_x, 1 ) ;
   
   call execute( 'rslt = x_mat', dyadic_fcn, 'y_mat ;' ) ;
   
   return rslt ;
finish o_p ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x = 1 : 5 ;
y = 1 : 3 ;

z1 = o_p( '&amp;lt;', x, y ) ;
z2 = o_p( '+', x, y ) ;
z3 = o_p( '^=', x, y ) ;
z4 = o_p( '#', x, y ) ;

print x y, z1[ l="dyadic function =' '&amp;lt;'"] " " z2[l="dyadic function = '+'"] " " z3[l="dyadic function = '^='"] " " z4[l="dyadic function = '#'"] ;

z = o_p( '#', 1, { 2 3 4 5 } ) ;
print z ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;produces the results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x 	  	y 	 
1 	2 	3 	4 	5 	1 	2 	3
dyadic function =' '&amp;lt;' 	  	dyadic function = '+' 	  	dyadic function = '^=' 	  	dyadic function = '#' 	 
0 	1 	1 	  	2 	3 	4 	  	0 	1 	1 	  	1 	2 	3
0 	0 	1 	  	3 	4 	5 	  	1 	0 	1 	  	2 	4 	6
0 	0 	0 	  	4 	5 	6 	  	1 	1 	0 	  	3 	6 	9
0 	0 	0 	  	5 	6 	7 	  	1 	1 	1 	  	4 	8 	12
0 	0 	0 	  	6 	7 	8 	  	1 	1 	1 	  	5 	10 	15
z
2 	3 	4 	5&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have solved the problem using conformable matrices, but it would be nice if there were an IML function to perform the same task more efficiently. I also know that I can use the APPLY function, but I have to define the particular dyadic function for two arguments, and this is an inelegant and inefficient use of APPLY, IMHO. The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  z = colvec( x ) * rowvec( y ) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;produces the same result as z = o_p( '#', x, y ) but this is the only case of a built-in IML outer product function that I know of. Is there any equivalent in IML for general dyadic functions?&lt;/P&gt;</description>
    <pubDate>Tue, 01 Aug 2023 18:39:34 GMT</pubDate>
    <dc:creator>rbettinger</dc:creator>
    <dc:date>2023-08-01T18:39:34Z</dc:date>
    <item>
      <title>Outer product capability for dyadic functions in IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Outer-product-capability-for-dyadic-functions-in-IML/m-p/887369#M6009</link>
      <description>&lt;P&gt;I have written an IML module to compute a table created by the application of a dyadic function ( '+', '#', '/', etc.) applied to two numeric vectors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start o_p( dyadic_fcn, x, y ) ;
   /* purpose: compute outer product of vectors x, y by applying dyadic function
    *
    * parameters:
    *    dyadic_fcn ::= quoted dyadic function, e.g., + - = ^= # ## &amp;lt; &amp;gt; , &amp;amp;cetera
    *    x          ::= numeric scalar or vector
    *    y          ::= numeric scalar or vector
    *
    * returns outer product of column vector x &amp;lt;dyadic function&amp;gt; row vector y
    *
    *  example:
    *    a = 1 : 4 ; b = 1 : 5 ;
    *
    *    c = op( '/', a, b ) ;
    */

   tmp_x = colvec( x ) ; n_row_x = nrow( tmp_x ) ; 
   tmp_y = rowvec( y ) ; n_col_y = ncol( tmp_y ) ;

   x_mat = repeat( tmp_x, 1, n_col_y ) ;&lt;BR /&gt;   y_mat = repeat( tmp_y, n_row_x, 1 ) ;
   
   call execute( 'rslt = x_mat', dyadic_fcn, 'y_mat ;' ) ;
   
   return rslt ;
finish o_p ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x = 1 : 5 ;
y = 1 : 3 ;

z1 = o_p( '&amp;lt;', x, y ) ;
z2 = o_p( '+', x, y ) ;
z3 = o_p( '^=', x, y ) ;
z4 = o_p( '#', x, y ) ;

print x y, z1[ l="dyadic function =' '&amp;lt;'"] " " z2[l="dyadic function = '+'"] " " z3[l="dyadic function = '^='"] " " z4[l="dyadic function = '#'"] ;

z = o_p( '#', 1, { 2 3 4 5 } ) ;
print z ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;produces the results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x 	  	y 	 
1 	2 	3 	4 	5 	1 	2 	3
dyadic function =' '&amp;lt;' 	  	dyadic function = '+' 	  	dyadic function = '^=' 	  	dyadic function = '#' 	 
0 	1 	1 	  	2 	3 	4 	  	0 	1 	1 	  	1 	2 	3
0 	0 	1 	  	3 	4 	5 	  	1 	0 	1 	  	2 	4 	6
0 	0 	0 	  	4 	5 	6 	  	1 	1 	0 	  	3 	6 	9
0 	0 	0 	  	5 	6 	7 	  	1 	1 	1 	  	4 	8 	12
0 	0 	0 	  	6 	7 	8 	  	1 	1 	1 	  	5 	10 	15
z
2 	3 	4 	5&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have solved the problem using conformable matrices, but it would be nice if there were an IML function to perform the same task more efficiently. I also know that I can use the APPLY function, but I have to define the particular dyadic function for two arguments, and this is an inelegant and inefficient use of APPLY, IMHO. The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  z = colvec( x ) * rowvec( y ) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;produces the same result as z = o_p( '#', x, y ) but this is the only case of a built-in IML outer product function that I know of. Is there any equivalent in IML for general dyadic functions?&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 18:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Outer-product-capability-for-dyadic-functions-in-IML/m-p/887369#M6009</guid>
      <dc:creator>rbettinger</dc:creator>
      <dc:date>2023-08-01T18:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: Outer product capability for dyadic functions in IML</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Outer-product-capability-for-dyadic-functions-in-IML/m-p/887382#M6010</link>
      <description>&lt;P&gt;Interesting. I haven't seen the term 'dyadic function' use like this before. Looks like your 'dyadic functions' are the pairwise scalar operators&lt;/P&gt;
&lt;P&gt;D[i,j] = x[i] op y[j]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although I almost always recommend IML functions over SAS macros, but this might be a case where a macro is simpler:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
/* use macro instead of a function call */
%macro o_p(x, op, y);
    (colvec(&amp;amp;x) &amp;amp;op repeat(rowvec(&amp;amp;y),nrow(&amp;amp;x)*ncol(&amp;amp;x)))
%mend;

x = 1 : 5 ;
y = 1 : 3 ;
w1 = %o_p(x, &amp;lt;, y );
w2 = %o_p(x, +, y );
w3 = %o_p(x, ^=, y );
w4 = %o_p(x, #, y );
print w1[r=x c=y l="dyadic function = '&amp;lt;'"],
      w2[r=x c=y l="dyadic function = '+'"], 
      w3[r=x c=y l="dyadic function = '^='"],
      w4[r=x c=y l="dyadic function = '#'"];
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Aug 2023 20:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Outer-product-capability-for-dyadic-functions-in-IML/m-p/887382#M6010</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-08-01T20:13:50Z</dc:date>
    </item>
  </channel>
</rss>

