Re: More questions and a documentation bug
Yann LeCun <[email protected]>
| Newsgroups | gmane.lisp.lush.devel |
|---|---|
| Message-ID | <[email protected]> |
> Ok, I found the source of my lunar problems: On the web, the @@ characters
> aren't displayed.
Arghlp! I'll fix that.
> I'd be willing to give the HTML docs a CSS makeover if the Lush crew are
> interested. See keir.mierle.com for a bit of my CSS work if you're curious.
Hmm, I stayed away from CSS so far (relying on gtml macros),
but that might be a good idea if there are obvious benefits.
> Now, I've been trying to get the matrix stuff to work properly, with mixed
> results. The documentation mentions a 'scalar' function to aid in creating
> idx0's, but it's location eludes me. I rgrepped through the lsh, packages,
> and sys directories and didn't see anything.
That's one instance of the doc being ahead of the code.
scalar (if it were defined) would be nothing more than a
macro that expands to (double-matrix).
> Although I didn't look quite
> as hard, I didn't see the constant function either.
defconstant is defined in lsh/libc/constants.lsh
> And finally, is there a 'slice' function hidden away somewhere that can
> give me slicing similar to, say, Python's or Matlab's matrix manipulation?
> I am aware of select, but it only gets half-way there, not being nearly as
> expressive. Am I missing something?
>
> For instance, say i wanted to take a matrix, and rotate around all the
> rows, having the last one wrap to the first. With slicing, this is easy:
> (Numeric Python code example)
>
> rotated_cols = concat(m[1:], m[:1])
>
> where m[1:] all the rows excluding the first, and m[:1] is the first row.
> (See http://www.pfdubois.com/numpy/html2/numpy-6.html#marker-59620 for more
> details)
I will add a idx-concat function, because it's convenient.
Naturally, this kind manipulation is not very memory efficient,
but that's only a problem if you are manipulating very large matrices.
That said, narrow does what you want (as your next message indicates).
> With select, I would have to loop on a range from 1->n, accumulating the
> 'slices' into another matrix, then copy the first row to the last row of
> the accumulation matrix. So, is there a better way?
Yup, here is a function that does in-place line rotation:
(de rotate-cols (m)
((-idx2- (-double-)) m)
(let ((col (idx-copy (select m 1 0)))
(n (- (idx-dim m 1) 1)))
(idx-copy (narrow m 1 n 1) (narrow m 1 n 0))
(idx-copy col (select m 1 n)) m))
If you really want to go fast, and don't need to
process non-contiguous matrices, you can always do:
(de rotate-cols (m)
((-idx2- (-double-)) m)
(when (<> 1 (idx-modulo m 1)) (error "non contiguous matrix"))
(let ((n (idx-dim m 1)))
((-int-) n)
(idx-bloop ((m m))
#{{ double *p = IDX_PTR($m,double);
double first = p[0];
memmove(p,p+1,sizeof(double)*($n-1));
p[$n-1] = first;
} #})) m)
> However, narrow istill not quite as powerful as Matlab / Python slicing.
> Could narrow be extended to allow negative offsets?
I suppose we could design a set of slicing macros that
would make things a bit simpler.
-- Yann
-------------------------------------------------------
This sf.net email is sponsored by:
To learn the basics of securing your web site with SSL,
click here to get a FREE TRIAL of a Thawte Server Certificate:
http://www.gothawte.com/rd522.html