Does this help?  
Each line is a small amendment to the preceding one...
   (-:@(+/))3 4 5    NB. Semiperimeter, s
6
   (-:@(+/)-0&,)3 4 5  NB. s - 0, a, b, c
6 3 2 1
   (-:@(+/)*/@:-0&,)3 4 5  NB.  s * (s - a) * ...
36
   (-:@(+/)%:@(*/)@:-0&,)3 4 5  NB. Heron’s formula applied to 3 4 5
6
   (-:@(+/)%:@(*/)@:-0&,)  NB. Let interpreter remove unnecessary brackets...
-:@(+/) %:@(*/)@:- 0&,
So the semiperimeter is calculated just the once.  It relies on converting the 
triplet a,b,c to the quadruplet 0, a, b, c,  rather than doing particularly 
smart bracketing.

I don’t often use  [: but if you prefer it,  the following arises from a 
similar building process using [: rather than @ and @:

  ([:-:(+/))3 4 5
6
   (([:-:(+/)) - 0&,)3 4 5
6 3 2 1
   (([:-:(+/))( [: */ - )0&,)3 4 5
36
   (([:-:(+/))( [: %: [: */ - )0&,)3 4 5
6
   (([:-:(+/))( [: %: [: */ - )0&,)  NB. Get rid of extra brackets
([: -: +/) ([: %: [: */ -) 0&,

Cheers,
Mike


     
Sent from my iPad

> On 4 Sep 2018, at 12:50, Martin Kreuzer <[email protected]> wrote:
> 
> Hi all -
> 
> To calculate the area of a flat triangle, using Heron's formula,
> A(a,b,c)= sqrt( s2*(s2-a)*(s2-b)*(s2-c) )
> I wrote a simple function doing this:
> 
> * get the three sides (as list input y)
> * compute the half  perimeter s2
> * build the differences s2-y
> * build product
> * take square root
> 
> My explicit solution looks like this
> 
> taher=: 13 : '%: s2 * */ s2-y [ s2=. -: +/ y'
> 
> and works
> 
>   taher 3 4 5
> 6
> 
> Suggested tacit version looks like this (and works too)
> 
> tahert=: [: %: ([: -: +/) * [: */ ([: -: +/) - ]
> 
> Q: Is there a way to reference the intermediate result of ([: -: +/) the half 
> perimeter s2
> within the tacit expression, as has been done in the explicit..?
> [Guess the interpreter takes care of this anyway; my question aims at whether 
> a shorter formulation could be reached.]
> 
> Thanks
> -M
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to