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


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

Re: Why “new”?

From Ken Wesson <kwesson@gmail.com>
Subject Re: Why “new”?
Newsgroups comp.lang.java.programmer
References (2 earlier) <4d4cd875@news.x-privat.org> <4d4d617f$0$23763$14726298@news.sunsite.dk> <kLd3p.62928$ZS4.61193@newsfe07.iad> <4d4e3087@news.x-privat.org> <C4x3p.72003$Y8.16393@newsfe06.iad>
Message-ID <4d4f65e1@news.x-privat.org> (permalink)
Date 2011-02-07 04:24 +0100
Organization X-Privat.Org NNTP Server - http://www.x-privat.org

Show all headers | View raw


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. :)

Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web