Matrix#pretty_print available?

Hi folks,

Matrix lacks a method to return a string like (for
Matrix[[1,2], [3,4]]):

1 2
3 4

Has anybody written such a method that they could share?

Cheers,
Gavin

Hi –

···

On Sun, 19 Jan 2003, Gavin Sinclair wrote:

Hi folks,

Matrix lacks a method to return a string like (for
Matrix[[1,2], [3,4]]):

1 2
3 4

Has anybody written such a method that they could share?

Wouldn’t it be very hard to generalize that, since matrices can
contain arbitrary objects, and to arbitrary depth?

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Here you go - it’s two-pass, and doesn’t handle complex
(collection-type) elements or >2 dimensions, but it’s useful in the
common case. Insert to_a at appropriate places if you don’t want to mix
Enumerable into Vector.

require ‘matrix’

module Enumerable
def map_with_index
a =
each_with_index {|e, i| a << yield(e,i)}
a
end
alias collect_with_index map_with_index

def formatrow(separator, *widths)
map_with_index {|a,i|
w = widths[i]
(a.to_s).slice(0…(w-1)).ljust(w)
}.join(separator)
end
end

class Vector
include Enumerable
def each
size.times {|i| yield @elements[i]}
end

def each_with_index
size.times {|i| yield @elements[i], i}
end
end

class Matrix
def pp
#calculate column sizes
a, t = , 0
column_size.times {|j|
a << column(j).inject(0) {|max, i|
(t = i.to_s.length) > max ? t : max
}
}
(0…(row_size - 1)).map {|i| row(i).formatrow(’ ',*a)}.join(“\n”)
end
end

a = Matrix[[1,2,3],[4,5,6],[10,9,7]]
print a.pp

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

Hi folks,

Matrix lacks a method to return a string like (for
Matrix[[1,2], [3,4]]):

1 2
3 4

Has anybody written such a method that they could share?

I must say, I wasn’t expecting that. Maybe I missed a few too many
algebra lectures, but I thought matrices contained numbers. But
Matrix[[“x”, “y”]] has no complaint. Though performing many methods
on it does :slight_smile:

I don’t know what you mean to “an arbitrary depth”.

Anyway, it may be hard to generalize, but who really needs a
generalized case? Such a method would still be useful to 99% of users
of the class.

Gavin

···

On Sunday, January 19, 2003, 11:42:25 PM, dblack wrote:

Hi –

On Sun, 19 Jan 2003, Gavin Sinclair wrote:

Hi folks,

Matrix lacks a method to return a string like (for
Matrix[[1,2], [3,4]]):

1 2
3 4

Has anybody written such a method that they could share?

Wouldn’t it be very hard to generalize that, since matrices can
contain arbitrary objects, and to arbitrary depth?

Hi –

Hi –

Hi folks,

Matrix lacks a method to return a string like (for
Matrix[[1,2], [3,4]]):

1 2
3 4

Has anybody written such a method that they could share?

Wouldn’t it be very hard to generalize that, since matrices can
contain arbitrary objects, and to arbitrary depth?

I must say, I wasn’t expecting that. Maybe I missed a few too many
algebra lectures, but I thought matrices contained numbers. But
Matrix[[“x”, “y”]] has no complaint. Though performing many methods
on it does :slight_smile:

When it comes to math issues, a good first approximation is to assume
that I’m wrong :slight_smile:

I don’t know what you mean to “an arbitrary depth”.

I meant multi-dimensional matrices.

David

···

On Sun, 19 Jan 2003, Gavin Sinclair wrote:

On Sunday, January 19, 2003, 11:42:25 PM, dblack wrote:

On Sun, 19 Jan 2003, Gavin Sinclair wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hmm, sounds like Matrix needs to be refactored to support symbolic math.
:slight_smile:

···

On Sunday, 19 January 2003 at 22:30:35 +0900, dblack@candle.superlink.net wrote:

Hi –

I must say, I wasn’t expecting that. Maybe I missed a few too many
algebra lectures, but I thought matrices contained numbers. But
Matrix[[“x”, “y”]] has no complaint. Though performing many methods
on it does :slight_smile:


Jim Freeze

Positive, adj.:
Mistaken at the top of one’s voice.
– Ambrose Bierce, “The Devil’s Dictionary”

Hmm, sounds like Matrix needs to be refactored to support symbolic math.

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org


Just a little nit-picking here:

It’s my understanding that “refactoring” implies adding no new
functionality
; simply reorganizing what’s there. In fact, I thought that
was one of the first rules of refactoring: Don’t add functionality while
refactoring.

Maybe I just had too much coffee this morning…

:slight_smile:

Chris

From: “Jim Freeze” jim@freeze.org

Hmm, sounds like Matrix needs to be refactored to support symbolic math.

Just a little nit-picking here:

It’s my understanding that “refactoring” implies adding no new
functionality
; simply reorganizing what’s there. In fact, I thought
that was one of the first rules of refactoring: Don’t add functionality
while refactoring.

You are right. It is not nit-picking. People tend to use words that have
specific meaning in casual ways that dilute or change their meaning. This
is very bad in technical professions such as ours that requires precise
language. Language is value-laden and imprecise as it is without further
dilution of the meanings of words.

Not picking on you here Jim, I know it is easy to pick the wrong words, do
it myself. Just be careful.

···

On Mon, 20 Jan 2003 01:41:05 +0900 “Chris Pine” nemo@hellotree.com wrote:

----- Original Message -----

Maybe I just had too much coffee this morning…

:slight_smile:

Chris


“Daniel P. Zepeda” <daniel@z,e,p,e,d,a,-,z,o,n,e.net>
(Remove commas for address)

You are right. I said to my self right after I hit the send key
that I probably shouldn’t have used refactoring…
unless you consider that 1.add(2) does not add
new functionality to 1.add(1). So, ‘1+1’ and ‘1+2’ are
the same function, then (thinking like a mathematician)
why should ‘1+x’ be any different. :slight_smile:

Anyway, the point I was trying to get across (you have to step
out of the CS world for a moment) is that math is not all
numbers and scalars.

···

On Monday, 20 January 2003 at 1:41:05 +0900, Chris Pine wrote:

----- Original Message -----
From: “Jim Freeze” jim@freeze.org

Hmm, sounds like Matrix needs to be refactored to support symbolic math.

Just a little nit-picking here:

It’s my understanding that “refactoring” implies adding no new
functionality
; simply reorganizing what’s there. In fact, I thought that
was one of the first rules of refactoring: Don’t add functionality while
refactoring.


Jim Freeze

Mencken and Nathan’s Fifteenth Law of The Average American:
The worst actress in the company is always the manager’s wife.