Yes, I've just checked m14 - it rather looks as if I was plagiarising it!

It uses 0: , ] rather than my 0&,  ...
I haven't ever checked whether using a verb constant like 0: is
"better" than the noun 0 .  Early versions of J didn't permit
constants in forks, so 0 , ] wasn't allowed.

FWIW, if you want to represent root(s(s-a)(s-b)(s-c)) more directly,
here's another line by line development.  Indented lines are my
input, the left-adjusted lines are the interpreter's result.

   s =: -:@:(+/)                      NB. semiperimeterverb
   s 3 4 5                            NB. semiperimeter of a,b,c
6
   (s - ])3 4 5                       NB. s - (a, b, c)
3 2 1
   (s( */@: -  )])3 4 5               NB. (s-a)(s-b)(s-c)
6
   (s([ * */@: -  )])3 4 5            NB. s(s-a)(s-b)(s-c)
36
   (s([ %:@* */@: -  )])3 4 5         NB. Root[s(s-a)(s-b)(s-c)]
6
   (s([ %:@* */@: -  )])              NB. Tacit expression
s ([ %:@* */@:-) ]
   (s([ %:@* */@: -  )]) f.           NB. Expanded form
-:@:(+/) ([ %:@* */@:-) ]

As I hope you can see, s is evaluated just the once, and then applied
as the left hand argument with a,b,c as the right hand argument.

Cheers,

Mike



On 04/09/2018 23:11, Martin Kreuzer wrote:
@Mike

Yes, it does help, as
it is a sort of eye-opener, rephrasing the fourth factor (s) as (s-0), and
it answers my (yet un-uttered) question about "m14".

-M


At 2018-09-04 13:46, 'Mike Day' via Programming wrote:

Does this help?
Each line is a small amendment to the preceding one...
   (-:@(+/))3 4 5    NB. Semiperimeter, s

   (-:@(+/)-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

   (-:@(+/)%:@(*/)@:-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

   (([:-:(+/)) - 0&,)3 4 5
6 3 2 1
   (([:-:(+/))( [: */ - )0&,)3 4 5
36
   (([:-:(+/))( [: %: [: */ - )0&,)3 4 5

   (([:-:(+/))( [: %: [: */ - )0&,)  NB. Get rid of extra brackets
([: -: +/) ([: %: [: */ -) 0&,

Cheers,
Mike



Sent from my iPad

> On 4 Sep 2018, at 12:50, Martin Kreuzer <i...@airkreuzer.com> 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

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to