[Jprogramming] newbie help: how to avoid division by zero
Linda Alvord
lindaalvord at verizon.net
Sun May 19 14:57:52 UTC 2013
I've been enjoying your link to Roger Stokes's chapter. But....
odd=:2 & |
halve=:-:
collatz =: 3 : 'if. odd y do. 1 + 3 * y else. halve y end.'
veco =: 3 : '(c*1+3*y) + (halve y) * (1-c =. odd y)'
data =: 1 + i. 10000
(collatz"0 data) -: (veco data)
1
data=:12
(collatz"0 data) -: (veco data) NB. OK
1
(collatz"0 12) -: (veco 12) NB. What is this? Should be 0 or 1
.
10
(collatz 12) -: (veco 12) NB. OK
1
Linda
-----Original Message-----
From: programming-bounces at forums.jsoftware.com
[mailto:programming-bounces at forums.jsoftware.com] On Behalf Of Jan-Pieter
Jacobs
Sent: Sunday, May 19, 2013 8:55 AM
To: programming at jsoftware.com
Subject: Re: [Jprogramming] newbie help: how to avoid division by zero
I think pointing people here:
http://www.rogerstokes.free-online.co.uk/31.htm might be good start.
It touches exactly these topics of replacing repeated scalar operations with
vector operations.
These techniques are applicable in any other language which is able to do
vector operations directly (eg. Matlab).
I hope this is the next version of the docs in preparation? It seems that
the version of Learning J on the jsoftware.com does not contain this chapter
(yet).
Jan-Pieter
2013/5/19 Linda Alvord <lindaalvord at verizon.net>:
> I think you missed my point.
>
> In the vocabulary in "Control Structures" you provide hints to formulate
this:
>
> if. y<0 do. c elseif. y>:0 do. d end.
>
> A beginner who finds this is likely to assume that this is the best way to
provide a conditional solution in J.
>
> Sadly, they are likely to work to master the use of this structure. A
better way is not there for them to see. I'm suggesting that a better way
(or the best known option at the current time) be provided right there.
>
> c*y<0)+(d*y>:0)
>
> Often these better ways appear on wiki pages. They are only brought to
light by some link on the forum which would otherwise never be found.
>
> If gems like this one were to appear in the vocabulary section it would
encourage faster development of good writing skills.
>
> So much of the give and take of the forum reduces concepts to a simpler
way of solving problems. It would be great if those "better ways" could find
their way into the vocabulary.
>
> Linda
>
> -----Original Message-----
> From: programming-bounces at forums.jsoftware.com
> [mailto:programming-bounces at forums.jsoftware.com] On Behalf Of Roger
> Hui
> Sent: Saturday, May 18, 2013 7:03 PM
> To: Programming forum
> Subject: Re: [Jprogramming] newbie help: how to avoid division by zero
>
> We don't do anyone any favors by providin
> if. y<0 do. c elseif. y>:0 do. d end.
> instead of
> c*y<0)+(d*y>:0)
>
>
> For one thing, the former has to work scalar-by-scalar, and for now and
for the forseeable future it'll be orders of magnitude slower that the
latter expression.
>
>> There are many places where newer and better ways of doing things
>> could
> be added to save lots of new users many pitfalls.
>
> Yes, we have newer and better ways of doing things; it's called array
thinking, and booleans returning 0 and 1 instead of false and true, and ...
>
>
>
> On Sat, May 18, 2013 at 1:41 PM, Linda Alvord
<lindaalvord at verizon.net>wrote:
>
>> try=: 3 : 0
>> c=:12
>> d=:14
>> if. c*y<0 do. c elseif. d*y>:0 do. d end.
>> )
>> try _3
>> 12
>> try 0
>> 14
>> try 3
>> 14
>>
>> c=:12
>> d=:14
>> y=:_3
>> (c*y<0)+(d*y>:0)
>> 12
>> x=:0
>> (c*y<0)+(d*y>:0)
>> 12
>> x=:3
>> (c*y<0)+(d*y>:0)
>> 12
>>
>> Roger, Most people seem to go first to the vocabulary to find a
>> starting place to solve their problems.
>>
>> Wouldn't it be useful to go to "Control Structures" to find a major
>> improvement such as the one you just wrote:
>>
>> if. c*y<0 do. c elseif. d*y>:0 do. d end.
>> or:
>> (c*y<0)+(d*y>:0)
>>
>> There are many places where newer and better ways of doing things
>> could be added to save lots of new users many pitfalls.
>>
>> Linda
>>
> ----------------------------------------------------------------------
> 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
More information about the Programming
mailing list