Feature #10072

[PATCH] Implement Vector.basis

Added by gogo tanaka about 1 year ago. Updated 11 months ago.

[ruby-core:63858]
Status:Closed
Priority:Normal
Assignee:Marc-Andre Lafortune

Description

Standard basis vectors are really important in linear algebra.

And we usually need this when we use matrix or vector. (base conversion, principal component analysis...

This is why I implemented.

But I recognize Ruby should be not for mathematician but rubyist.

Ruby doesn't need too academic method.

So If you think it is too academic you can ignore my patches.

gogo.

implement_Vector.basis.patch Magnifier (1.59 KB) gogo tanaka, 07/19/2014 07:15 AM

Associated revisions

Revision 47833
Added by Marc-Andre Lafortune 11 months ago

  • lib/matrix.rb: Add Vector.basis. Based on patch by gogo tanaka [#10072]

Revision 47833
Added by Marc-Andre Lafortune 11 months ago

  • lib/matrix.rb: Add Vector.basis. Based on patch by gogo tanaka [#10072]

History

#1 Updated by gogo tanaka about 1 year ago

We can build standard euclidean space easily with this method.

[Vector.basis(3, 0), Vector.basis(3, 1), Vector.basis(3, 2)]
=> [Vector[1, 0, 0],  Vector[0, 1, 0],  Vector[1, 0, 1]]

This patch is needed in https://bugs.ruby-lang.org/issues/10074

gogo.

#2 Updated by Hiroshi SHIBATA about 1 year ago

  • Category set to lib
  • Status changed from Open to Assigned
  • Assignee set to Marc-Andre Lafortune
  • Target version set to current: 2.2.0

#3 Updated by Marc-Andre Lafortune 11 months ago

Looks good, but I'm wondering if we should use named arguments as it's really difficult to remember the order without looking it up.

Vector.basis(3, 0)
# or
Vector.basis(size: 3, index: 0)

Not sure.

#4 Updated by gogo tanaka 11 months ago

@Marc-Andre Lafortune

I get your point. I admire your taste.

After some agonizing, I come up with implementing Vector.new (like Array.new) also.
And implement Vector#basis as for index.
So I mean, how about using Vector.new(3).basis(0) instead of Vector.basis(3, 0) ?

Just idea.

#5 Updated by Marc-Andre Lafortune 11 months ago

Interesting idea, but it has issues, like private new currently requiring an array, and it being two steps, the first one not too clear.

I think it's best to pick between the two possibilities I gave.

#6 Updated by Martin Dürst 11 months ago

On 2014/10/03 05:51, ruby-core@marc-andre.ca wrote:

Issue #10072 has been updated by Marc-Andre Lafortune.

Looks good, but I'm wondering if we should use named arguments as it's really difficult to remember the order without looking it up.

 Vector.basis(3, 0)
 # or
 Vector.basis(size: 3, index: 0)

Not sure.

Why not allow both? Ruby is flexible.

Regards, Martin.

#7 Updated by Marc-Andre Lafortune 11 months ago

  • Status changed from Assigned to Closed

Martin Dürst wrote:

Why not allow both? Ruby is flexible.

Indeed.
In this case though, it would be a bit of an overkill I think.
Let's use named arguments. If basis becomes really popular and we want to make a shortcut we still can.

#8 Updated by gogo tanaka 11 months ago

@Martin Dürst @Marc-Andre Lafortune

I agree it. Thank you so much.

Also available in: Atom PDF