On 04/08/2014 02:21 PM, John Clements wrote:

On Apr 8, 2014, at 12:04 PM, John Clements <[email protected]> wrote:


On Apr 8, 2014, at 11:54 AM, Sam Tobin-Hochstadt <[email protected]> wrote:

On Tue, Apr 8, 2014 at 2:48 PM, John Clements <[email protected]> wrote:
Here’s a short piece of code that tries to plot an array:

#lang typed/racket

(require plot
        math/array)

(define fft-maxes (array #[3.2 978.9 -2397]))

(: with-indexes (Vectorof (Vector Nonnegative-Integer Real)))
(define with-indexes
(for/vector : (Vectorof (Vector Nonnegative-Integer Real))
   ([i : Nonnegative-Integer (in-naturals)]
    [a (in-array fft-maxes)])
   (ann (vector (ann i Nonnegative-Integer) a)
        (Vector Nonnegative-Integer Real))))


(plot (lines (in-vector with-indexes)))

I have two questions.
1) Is there an easier way to pair an array with indices? This was kind of 
challenging.

Try `in-indexed`.

But… in-indexed produces a sequence of 2-value elements. I guess I can get it 
working with sequence-map:

(plot (lines (sequence-map (lambda (a b) (vector b a))
                           (in-indexed (in-array fft-maxes)))))

BUT WAIT! no, that doesn’t work in the typed setting, because sequence-map’s 
type doesn’t work for 2-valued sequences. Back to square 1.

Got it… “in-values-sequence”.

John

Four more options:

(plot (lines (for/list : (Listof (List Index Real))
                 ([js  (in-array-indexes (array-shape fft-maxes))]
                  [x   (in-array fft-maxes)])
               (list (vector-ref js 0) x))))


(plot (lines (array->list
              (array-map (λ ([js : Indexes] [x : Real])
                           (list (vector-ref js 0) x))
                         (indexes-array (array-shape fft-maxes))
                         fft-maxes))))

(plot (lines
       (matrix->list*
        (list-array->array
         (for/array ([js  (in-array-indexes (array-shape fft-maxes))]
                     [x   (in-array fft-maxes)]) : (List Index Real)
                    (list (vector-ref js 0) x))
         1))))

(plot (lines (matrix->list*
              (array-list->array
               (list (build-array (array-shape fft-maxes)
                                  (λ ([js : Indexes])
                                    (vector-ref js 0)))
                     fft-maxes)
               1))))

The last two use `matrix->list*' because there's no way for TR to prove that `array->list*' produces a (Listof (Listof t)). (Nothing about an array's shape is part of the type.)

Neil ⊥

____________________
 Racket Users list:
 http://lists.racket-lang.org/users

Reply via email to