[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: dot-product operators and strides for linear algebra

Logan Capaldo

2/16/2006 11:20:00 PM


On Feb 16, 2006, at 5:59 PM, <jam5238-001@yahoo.de> wrote:

> My assumption is that is might be possible to extend ruby with
> reasonable effort to add the additional ".*" "./" and ".^2" binary
> operators and the transpose / conjugate transpose operators. For
> the standard ruby this function would just do the same as their
> scalar conterparts, however, when implementing a matrix class
> these additional operators can become alive and be used to
> implement the elementwise dot operations.

That's an especially poor choice of operator name ;)

a * b == a.*(b)

You could add a method called .*, but you couldn't call it in the
normal manner, you'd have to say:
a.send(".*", b)
which I don't think is what you were looking for. As for "strides",
have you seen ranges?

0..9 # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
1...10 # 1, 2, 3, 4, 5, 6, 7, 8, 9

As for having an alternative step, you can use #step,

1.step(10, 2) { |x| p x }
1
3
5
7
9

To turn a step into a range-like object (instead of a method) use an
enumerator:

require 'enumerator'
zero_to_twentyfive_by_five = 0.to_enum(:step, 25, 5)

zero_to_twentyfive_by_five.each { |x| p x }
0
5
10
15
20
25

In 1.9 you don't even need to use the to_enum, ie you could simply write

zero_to_twentyfive_by_five = 0.step(25,5)





2 Answers

jam5238-001

2/17/2006 8:38:00 AM

0

1) Operator/Method Abiguity: Clearly, this is a problem. I don't think "*" or the like operators would hurt too much though. It would be necessary to forbid "class.*method" style of methods and therefore a major design issue.

2) Ranges: This is a good suggestion, but how to extend it to possibly n-dimensional matrices?
I am not an expert on the implementation of ranges, by I suspect that ranges are executed by the interpreter. Strides can be executed by the matrix library and implemented more efficiently in C. The steps method, constructs an array immediately, this is not necessary for strides. If a stride is used for indexing it is only necessary to hand over the start, step and end index. Only if a stride is assigned to a vector, then it is necessary to convert it to an array.

Regards,

Roderick


Logan Capaldo <logancapaldo@gmail.com> schrieb:
On Feb 16, 2006, at 5:59 PM, wrote:

> My assumption is that is might be possible to extend ruby with
> reasonable effort to add the additional ".*" "./" and ".^2" binary
> operators and the transpose / conjugate transpose operators. For
> the standard ruby this function would just do the same as their
> scalar conterparts, however, when implementing a matrix class
> these additional operators can become alive and be used to
> implement the elementwise dot operations.

That's an especially poor choice of operator name ;)

a * b == a.*(b)

You could add a method called .*, but you couldn't call it in the
normal manner, you'd have to say:
a.send(".*", b)
which I don't think is what you were looking for. As for "strides",
have you seen ranges?

0..9 # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
1...10 # 1, 2, 3, 4, 5, 6, 7, 8, 9

As for having an alternative step, you can use #step,

1.step(10, 2) { |x| p x }
1
3
5
7
9

To turn a step into a range-like object (instead of a method) use an
enumerator:

require 'enumerator'
zero_to_twentyfive_by_five = 0.to_enum(:step, 25, 5)

zero_to_twentyfive_by_five.each { |x| p x }
0
5
10
15
20
25

In 1.9 you don't even need to use the to_enum, ie you could simply write

zero_to_twentyfive_by_five = 0.step(25,5)








---------------------------------
Telefonieren Sie ohne weitere Kosten mit Ihren Freunden von PC zu PC!
Jetzt Yahoo! Messenger installieren!

Logan Capaldo

2/17/2006 4:08:00 PM

0


On Feb 17, 2006, at 3:37 AM, <jam5238-001@yahoo.de>
<jam5238-001@yahoo.de> wrote:

> The steps method, constructs an array immediately, this is not
> necessary for strides.

Not true, step does not construct an array.