Mathematica and Dyalog
5 posts
• Page 1 of 1
Mathematica and Dyalog
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.
"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
Nice... Note that you can also write this in a couple of more direct ways :)
- Code: Select all
+/100<10?200
100+.<10?200
-
Morten|Dyalog - Posts: 462
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Mathematica and Dyalog
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.
-
StefanoLanzavecchia - Posts: 113
- Joined: Fri Oct 03, 2008 9:37 am
Re: Mathematica and Dyalog
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
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
5 posts
• Page 1 of 1
Return to Functional Programming
Who is online
Users browsing this forum: No registered users and 0 guests
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group