Newsgroups: comp.lang.apl
Path: watmath!watserv1!72740.1026@compuserve.com
From: Bob Hendricks <72740.1026@CompuServe.COM>
Subject: Re: NPV question
Message-ID: <920303220423_72740.1026_EHE29-1@CompuServe.COM>
Sender: root@watserv1.waterloo.edu (Operator)
Organization: University of Waterloo
Date: Tue, 3 Mar 1992 22:04:24 GMT
Lines: 55

On March 2, Peter Petto (ppetto@NCoast.ORG) asked:

   I am looking for an answer to a problem I am having trying to define
   a function in J.  Right now I have a function that is the central to
   a variety of discounted cash flow calculations. It is:

       npv=.'+/y.' : '+/(y.%(1+x.)^(#\y.))'

   And yields results such as the following:

       0.05 npv 1000 1200 1500 900
   4077
       0.06 npv 1000 1200 1500 900
   3983.71

   I would like to define a "mystery function" that will apply a set of
   left-side values to the same right-side values, yielding results as
   follows:

       0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 mystfn 1000 1200 1500 900
   4077 3983.71 3893.76 3807.01 3723.31 3642.51 3564.49 3489.13

   I know I'm missing something -- I'm just not sure what.  I'd appreciate
   any and all advice.

NB. Well, the easy answer is to use the rank operator to get a semi-outer
NB. product.

      p=. 1000 1200 1500 900
      i=. 100%~5+i. 8
      i
0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12
      p
1000 1200 1500 900
      npv=. '+/y.' : '+/(y.%(1+x.)^(#\y.))'

      0.05 npv p
4077
      i npv "0 1 p   NB. here is the key
4077 3983.71 3893.76 3807.01 3723.31 3642.51 3564.49 3489.13

NB. Here is a slight variation on creating the exponents.

      npv=. '+/y.' : '+/y.%(1+x.)^1+i. #y.'
      i npv "0 1 p
4077 3983.71 3893.76 3807.01 3723.31 3642.51 3564.49 3489.13

NB. Here is a different way altogether which uses the verb base.

      npvbase =. '+/y.' : '(%1+x.)#. "0 1 (|. 0, y.)'
      i npvbase p
4077 3983.71 3893.76 3807.01 3723.31 3642.51 3564.49 3489.13

Bob Hendricks

