[Jprogramming] Square Roots and Extended Arithmetic
Skip Cave
skip at caveconsulting.com
Sat Nov 3 04:25:01 UTC 2018
Linda,
NB. Generate the integers from 2 to 21, store them in a, and display a:
]a=.2+i.20
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
NB. Take the square root of the integers in a, store them in b, and display
b:
]b=.%:a
1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228 3.31662 3.4641
3.60555 3.74166 3.87298 4 4.12311 4.24264 4.3589 4.47214 4.58258
NB. Find the ceiling of b (which contains all the square roots of a)
>.b
2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5
NB. Mark all the places where the ceiling of b is the same as b, and
display the mark vector.
NB. This essentially marks every number in b that has no fractional part,
i.e the numbers in b that are integers.
]m=. b = >. b
0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
NB. this is the same as:
(=>.) b
0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
NB. Now we can use the mark vector to display only the numbers in b that
are integers:
m#b
2 3 4
NB: Or we can use the mark vector to select all the integers from a that
have integer square roots:
m#a
4 9 16
NB. Now, like most J expressions, we can do either of these things all in
one line:
b#~(=>.)%:a=.2+i.20
2 3 4
a#~(=>.)%:a=.2+i.20
4 9 16
Skip
Skip Cave
Cave Consulting LLC
On Fri, Nov 2, 2018 at 9:25 PM Linda Alvord <lindaalvord36 at outlook.com>
wrote:
> What does (=>.) do in this example?
>> Linda
>>>> Sent from my Verizon, Samsung Galaxy smartphone
>>> -------- Original message --------
> From: Skip Cave <skip at caveconsulting.com>
> Date: 11/2/18 12:06 PM (GMT-05:00)
> To: "Programming at jsoftware.com" <programming at jsoftware.com>
> Subject: Re: [Jprogramming] Square Roots and Extended Arithmetic
>> Thanks for all the comments and help in understanding why trying to get an
> extended integer result from a square root is a bad idea.
>> I think the functional symmetry of square *: and square root %: led me to
> subconsciously forget that the results of squared integers (all integers)
> are not symmetrical with the results of the square roots of integers (which
> are NOT all integers). I should remember this, since one of my favorite J
> idioms is the implicit verb (=>.) for finding integers in a list of
> floating point numbers:
>> a=.2+i.20
>> %:a
>> 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228 3.31662 3.4641
> 3.60555 3.74166 3.87298 4 4.12311 4.24264 4.3589 4.47214 4.58258
>> (=>.)%:a
>> 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
>> a#~(=>.)%:a
>> 4 9 16
>> Skip
> ----------------------------------------------------------------------
> For information about J forums see
> https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.jsoftware.com%2Fforums.htm&data=02%7C01%7C%7Cff92971a609a479e20a408d640dd2c50%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636767716000495561&sdata=G6T0f9%2BRqYnk1RdVs20bJpDQoL1LbSJfno1GhgqDoxg%3D&reserved=0
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list