Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25525 > unrolled thread

Re: Why “new”?

Started byKen Wesson <kwesson@gmail.com>
First post2011-02-07 04:24 +0100
Last post2011-02-07 04:24 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Why “new”? Ken Wesson <kwesson@gmail.com> - 2011-02-07 04:24 +0100

#25525 — Re: Why “new”?

FromKen Wesson <kwesson@gmail.com>
Date2011-02-07 04:24 +0100
SubjectRe: Why “new”?
Message-ID<4d4f65e1@news.x-privat.org>
On Sun, 06 Feb 2011 09:09:54 -0400, Arved Sandstrom wrote:

>> So the question is, what did you mean by bounds checking? If you meant
>> "make sure not to walk off the end of an array and crash" then they
>> won't crash. If you meant "make sure the 'matrix' isn't actually
>> ragged-right, and that matrices are compatible for multiplication" then
>> they won't throw exceptions on those conditions, and instead will
>> return probably- garbage results.
>
> The latter was what I meant. I don't consider a matrix multiplication
> language comparison example to be complete unless it demonstrates that
> kind of checking. I'm not anal about it - a bit of handwaving that says
> that a matrix type ("class" or similar) exists that takes care of some
> of the checking at construction time is cool. But you'd certainly want
> to show the compatibility check.

Eh. It's pretty rudimentary. For mmul you just throw in (assert (= (count 
m2) (count (first m1)))) to check compatibility, assuming m1 and m2 are 
rectangular. Add (assert (and (rectangular? m1) (rectangular? m2))) to 
check that first, if you wish, with

(defn rectangular? [m]
  (and
    (coll? m)
    (every? coll? m)
    (every? #(= (count (first m)) (count %)) m)))

This doesn't check for the elements being scalars but you'll get a type 
error from * in dotp if any are not after the other checks pass. The 
publicly accessible dotp may also want to have (assert (= (count v1) 
(count v2))).

Alternatively, just put the latter check in dotp and leave mmul as just 
(map #(map (partial dotp %) (transpose m2)) m1) and if there's any 
incompatibility or nonrectangularity one of the dotp calls will 
inevitably throw. But it will do extra traversals to count lengths. Of 
course if we wanted performance we'd be using an optimized linalg lib to 
begin with so consider that moot. :)

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web