Mathematica and Dyalog

For users of dfns, both novice and expert

Mathematica and Dyalog

Postby mwr0707 on Fri Dec 01, 2023 1:01 am

I was experimenting with the Free Wolfram Engine today and tried the following problem:
"Count the number of elements in a list of 10 random integers between 0 and 199 that are greater than 100"

This worked in the free engine:
Total[Boole[Map[(#>100)&,RandomInteger[200,10],1]]]

A Dyalog equivalent is:
(+/>∘100) 10?200

One of the things that jumps out at me about how APL simplifies is that APL implicitly applies Map[] to right function arguments. And Boolean to Integer conversion is not needed. So nice. I love APL.
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am

Re: Mathematica and Dyalog

Postby Morten|Dyalog on Fri Dec 01, 2023 7:47 am

Nice... Note that you can also write this in a couple of more direct ways :)

Code: Select all
+/100<10?200
100+.<10?200
User avatar
Morten|Dyalog
 
Posts: 458
Joined: Tue Sep 09, 2008 3:52 pm

Re: Mathematica and Dyalog

Postby StefanoLanzavecchia on Fri Dec 01, 2023 8:18 am

Please notice that Mathematica's RandomInteger[max,n] returns n number in the range 0..max, possibly with repetitions, whereas dyadic deal (
      x?y
) returns a partial permutation of the array
      ⍳y
. https://aplwiki.com/wiki/Deal. To replicate the behaviour of Mathematica's RandomInteger just apply roll (?x) to n copies of x:
      ?n⍴x
.
User avatar
StefanoLanzavecchia
 
Posts: 113
Joined: Fri Oct 03, 2008 9:37 am

Re: Mathematica and Dyalog

Postby mwr0707 on Fri Dec 01, 2023 11:18 am

Morten|Dyalog wrote:Nice... Note that you can also write this in a couple of more direct ways :)

Code: Select all
+/100<10?200
100+.<10?200


I had stumbled on that (reversing the comparison order) earlier, but I had also just watched Marshall Lochbaum's presentation on Tacit techniques to Dyalog'19. So I was thinking about sacrificing some conciseness to preserve the original problem context for the non-expert code reader (me). I've noticed that John Scholes addressed this trade-off with liberal use of comments.

It occurred to me that the conciseness offered by Dyalog and tacit techniques over Mathematica can actually improve comprehension of the original problem context. Of course, comprehensibility depends on the experience and skill level of the reader. Over the decades, the Dyalog team has skillfully balanced technical and communication techniques (what I think Iverson as a teacher intended) for the skill level of the intended audience.

Thank-you. Well done.
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am

Re: Mathematica and Dyalog

Postby mwr0707 on Fri Dec 01, 2023 11:19 am

StefanoLanzavecchia wrote:Please notice that Mathematica's RandomInteger[max,n] returns n number in the range 0..max, possibly with repetitions, whereas dyadic deal (
      x?y
) returns a partial permutation of the array
      ⍳y
. https://aplwiki.com/wiki/Deal. To replicate the behaviour of Mathematica's RandomInteger just apply roll (?x) to n copies of x:
      ?n⍴x
.


Good catch!
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am


Return to Functional Programming

Who is online

Users browsing this forum: No registered users and 1 guest