Project

General

Profile

Feature #10072

[PATCH] Implement Vector.basis

Added by gogotanaka (Kazuki Tanaka) almost 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Target version:
[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.


Files

implement_Vector.basis.patch (1.59 KB) implement_Vector.basis.patch gogotanaka (Kazuki Tanaka), 07/19/2014 07:15 AM

Updated by gogotanaka (Kazuki Tanaka) almost 6 years 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.

Updated by hsbt (Hiroshi SHIBATA) almost 6 years ago

  • Category set to lib
  • Status changed from Open to Assigned
  • Assignee set to marcandre (Marc-Andre Lafortune)
  • Target version set to 2.2.0

Updated by marcandre (Marc-Andre Lafortune) over 5 years 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.

Updated by gogotanaka (Kazuki Tanaka) over 5 years 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.

Updated by marcandre (Marc-Andre Lafortune) over 5 years 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.

Updated by duerst (Martin Dürst) over 5 years 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.

Updated by marcandre (Marc-Andre Lafortune) over 5 years 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.

Updated by gogotanaka (Kazuki Tanaka) over 5 years ago

@Martin Dürst @Marc-Andre Lafortune

I agree it. Thank you so much.

Also available in: Atom PDF