Sorry I got carried away below, but in case the following is useful: To develop the train of Don's explanation a bit further: +/"1 ] 1 _1 1 *"1 x^/i.3 21 57
Given that we are feeding both verbs a row at a time, we can combine the ( +/ ) and the ( * ) as one operation using the conjunction ( @: ) 1 _1 1 +/@:*"1 x^/i.3 21 57 This now looks very much like matrix multiplication and indeed if we swap the arguments to ensure that lengths agree we can do just that: 1 _1 1 +/ .*~ x^/i.3 21 57 As Don points out though, ( p. ) simplifies this all considerably! Note that the dyadic ranks of the verb ( p. ) are 1 0 : p. b. 0 1 1 0 This means that the left argument is fed to p. by row (list of coefficients) while the right argument is fed by element (alternative values for x). If there is a single list of coefficients and multiple right arguments, each will be evaluated for that list of coefficients: 1 _1 1 p. 5 8 10 11 21 57 91 111 If there are multiple lists of coefficients and a single right argument, each list of coefficients will be evaluated for that right argument. (1 1 1 , 1 _1 1 ,: _1 1 _1) p. 5 31 21 _21 If the number of lists of coefficients matches the number of right arguments, then each list of coefficients is evaluated using its corresponding right argument. (1 1 1 , 1 _1 1 ,: _1 1 _1) p. 5 8 10 31 57 _91 If you want to evaluate all lists of coefficients for all right arguments then the following will suffice (1 1 1 , 1 _1 1 ,: _1 1 _1) p./ 5 8 10 31 73 111 21 57 91 _21 _57 _91 p./ is equivalent to p."1 _ If ( p. ) wasn't provided the following might be an alternative: evalPoly=: ([ +/ .*~ ] ^/ i...@#@[)"1 0 1 _1 1 evalPoly x 21 57 Another alternative is given on the wiki page: http://www.jsoftware.com/jwiki/Phrases/Polynomials eval=: ([: +`*/ [: }:@, ,"0)"1 0 1 _1 1 eval x 21 57 > From: Don Guinn > Sent: Friday, 20 August 2010 10:36 > > What you need to keep straight is the rank of the various parts. > Breaking > this into two parts, first evaluating the values for each value of x > against > the powers of x times the coefficients for each power: > > x=:5 8 > > 1 _1 1*"1 x^/i.3 > 1 _5 25 > 1 _8 64 > > Then sum each row. Here you have to override the rank of the sum: > > +/"1]1 _1 1*"1 x^/i.3 > 21 57 > > But you might prefer using p. which is much cleaner. > > 1 _1 1 p. x > 21 57 > > > On Thu, Aug 19, 2010 at 5:02 PM, Johann Hibschman wrote: > > > (I accidentally sent this to j.general, to which I'm not yet > subscribed; > > I don't expect that to go through, but if it does, apologies for the > > duplication.) > > > > > > Here's a simple question from a rank beginner: > > > > Expressing polynomials as, for example, > > > > +/1 _1 1*x^i.3 > > > > works well, as long as x is a scalar. If x is of higher rank, this > > stops working. My somewhat brute-force solution is to manually set > the > > rank used, as in: > > > > x=:2 3 5 > > +/1 _1 1*x^_ 0 i. 3 > > > > Is this good style? Is there a better way? ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
