Chapel: Create "one" method in Linear Algebra package

Created on 8 Dec 2017  路  8Comments  路  Source: chapel-lang/chapel

Simple and useful, create a ones(m) or ones(m,n) method like numpy's in the Linear Algebra package.

Libraries / Modules Won't fix / Ain't broke Feature Request user issue

Most helpful comment

@nspark: The concern you raise is also captured in issue #5958, also rapidly becoming known as "Brad's favorite issue"

All 8 comments

We decided against this, since this can be achieved easily in Chapel with promotion, e.g.

// ones(m)
var x = Vector(m);
x = 1;

// ones(m, n)
var A = Matrix(m, n);
A = 1;

or if using array semantics,

var x: [1..m] real = 1;
var A: [1..m, 1..n] real = 1;

Get me We on the phone right now and I'll explain to her that in the time it takes her to have you write this post she could have checked in a fix. And that just because Python didn't sign her yearbook doesn't mean the most popular data science languages (along with R and MatLab) aren't wrong on everything. I can't wait to hear what she has to say about zeroes()

Someone asked me to go all "professor" on this (it was Bruce) so let me expand. This is longer than it needs to be but hopefully the thinking will help.

An "algebra" is a set of objects with an operation defined on them. The integers are a set of numbers with for instance a +. So a + b produces another integer. That means the integers are "closed under the operation +". Each algebra also has an "identity" for the operation. In the integers, the additive identity is 0. That means a + 0 = a. The multiplicative identity is 1 since a*1 = a. The integers have two operations, + and * with identity members 0 and"1.

When working in an algebraic context, like when using R, Python, Matlab and Mathematica, you are not forced to think like a programmer and write a = [0,0,0,0], you simply think of algebraic objects. When I have a matrix and I want to do nothing to it, I think A + 0. This is a common scientific programming pattern: m = zeroes(3,3) to get a 3x3 matrix of zeroes. It's quick and clean. Same for 1. It preserves the algebraic mindset.

Because of that the very easy and convenient functions ones() and zeroes() are useful from a productivity standpoint.

ADDENDUM:
Note that the natural numbers 1,2,3.... do not have an additive inverse. They are not closed under "-" and this kinda why we had to make up the integers (it was brilliant, but I wasn't part of that decision). 2-5 is not a natural number. Also, the integers don't have a multiplicative inverse. If you divide 3 by 2, you don't get an integer. That's why we have the reals. There's another decision I wish I could have been part of.

@buddha314 I think you actually made a good argument for _favoring_ Chapel's implicit promotion. You say:

When I have a matrix and I want to do nothing to it, I think A + 0. [...] Same for 1. It preserves the algebraic mindset.

Using promotion, this is what Chapel will do for you (Try It Online):

config const n = 5;

var A = Matrix(n, n);
fillRandom(A);
writeln("A = \n", A);
writeln();

A += 1;
writeln("A = \n", A);

This looks like the "algebraic mindset" you want and seems less like a "programmer" mindset of making a function call with user-specified dimensions to return an object that, based on the name of the call, returns all zeros or all ones.

@ben-albrecht One thing I noticed about promotions (maybe this should be a separate issue?) is that they do not preserve the rank of the array (Try It Online):

config const n = 5;

var A = Matrix(n, n);
fillRandom(A);
var B = A + 1;

writeln("A = \n", A);
writeln();
writeln("B = \n", B);
writeln();
writeln(A.type:string); // output: [domain(2,int(64),false)] real(64)
writeln(B.type:string); // output: [domain(1,int(64),false)] real(64)

One thing I noticed about promotions (maybe this should be a separate issue?) is that they do not preserve the rank of the array

This is what we refer to as promotion-flattening, a result of a known shortcoming in the current leader-follower implementation, and should ideally be corrected with leader-follower 2.0.

Until it is fixed, there are some warnings and work-arounds for it in the documentation.

@nspark: The concern you raise is also captured in issue #5958, also rapidly becoming known as "Brad's favorite issue"

Don't you think it's hurtful to the other issues to play favorites?

Summarizing discussion:

Given the language-level support for setting an entire matrix or vector to a value, it doesn't make a lot of sense for Chapel's linear algebra library to pursue functions such as ones() or zeros().

With that said, I'm going to label this issue as won't fix and close it.

Was this page helpful?
0 / 5 - 0 ratings