Project

General

Profile

Feature #10072

[PATCH] Implement Vector.basis

Added by gogo tanaka over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
[ruby-core:63858]

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 over 1 year ago

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

Revision 47833
Added by Marc-Andre Lafortune over 1 year ago

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

History

#1 [ruby-core:63881] Updated by gogo tanaka over 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 [ruby-core:63901] Updated by Hiroshi SHIBATA over 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 [ruby-core:65378] Updated by Marc-Andre Lafortune over 1 year 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 [ruby-core:65395] Updated by gogo tanaka over 1 year 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 [ruby-core:65403] Updated by Marc-Andre Lafortune over 1 year 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 [ruby-core:65411] Updated by Martin Dürst over 1 year 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 [ruby-core:65490] Updated by Marc-Andre Lafortune over 1 year 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 [ruby-core:65499] Updated by gogo tanaka over 1 year ago

@Martin Dürst @Marc-Andre Lafortune

I agree it. Thank you so much.

Also available in: Atom PDF