【问题标题】:Racket: What does double colon (::) mean?Racket:双冒号 (::) 是什么意思?
【发布时间】:2018-07-13 00:00:13
【问题描述】:

刚接触 Racket,由于某种原因无法在任何地方的官方文档中找到它。双冒号是刚刚在这个(require math/array)库中使用,还是在Racket中普遍有用?

确实知道我正在尝试做类似于 python 的 numpy 索引 arr[i:j,k:m] = 1 的事情。因此,如果有一种不太复杂的方法可以将数组中的一堆值设置为相同的值,请告诉我

> (define arr (array->mutable-array (axis-index-array #(5 5) 1)))
> (array-slice-set! arr (list (:: 1 #f 2) (::)) (array 1))
> arr

- : (Mutable-Array Integer)

(mutable-array

 #[#[0 1 2 3 4]

   #[1 1 1 1 1]

   #[0 1 2 3 4]

   #[1 1 1 1 1]

   #[0 1 2 3 4]])
> (array-slice-set!
   arr (list (::) (:: 1 #f 2))
   (array-scale (array-slice-ref arr (list (::) (:: 1 #f 2))) -1))
> arr

- : (Mutable-Array Integer)

(mutable-array

 #[#[0 -1 2 -3 4]

   #[1 -1 1 -1 1]

   #[0 -1 2 -3 4]

   #[1 -1 1 -1 1]

   #[0 -1 2 -3 4]])

【问题讨论】:

    标签: python numpy lisp racket


    【解决方案1】:

    这里有一个扩展的切片示例:

    https://docs.racket-lang.org/math/array_slicing.html

    虽然找不到任何将切片设置为常量的东西。 我继续实施了这样的事情,见下文。

    首先让我们尝试一些例子。

    #lang racket
    (require math/array)
    
    ;;; 1d array
    
    ; xc : vector -> integer
    ;  get first coordinate (the "x coordinate")
    (define (xc v) 
      (vector-ref v 0))
    
    ;; Build a 1d array with elements 0, 1, ..., 9
    (define A
      (array->mutable-array
       (build-array
        #(10)  ; one axis (the x-axis) range 0, 1, ..., 9
        xc)))  ; use index as-is
    
    ;; Show the 1d array
    A
    
    ;; Let's set the middle part to zero.
    ;; First we make a slice
    
    (:: 4 7)
    
    ;; Then we use it to set elements in the arrau
    
    (array-slice-set! A (list (:: 4 7)) (array #[0 0 0]))
    A ; now the tree middle elements are 0
    
    ;; Rather than write the zero array our-selves we can use make-array.
    (array-slice-set! A (list (:: 4 7)) (make-array #(3) 1))
    A
    
    ;;; nd array
    
    (define (coordinates v) (vector->list v))
    
    (define B
      (array->mutable-array
       (build-array
        #(4 4 4)  ; three axes of size 4
        coordinates)))
    
    B
    
    ;;; Let set the all entries except the "edge" ones to (x x x)
    
    (array-slice-set! B (list (:: 1 3) (:: 1 3) (:: 1 3))
                      (make-array (vector (- 3 1) (- 3 1) (- 3 1)) '(x x x)))
    B
    
    ;;; Now to avoid constructing the constant array, we will use the
    ;;; array-slice-set-constant! see below.
    
    (require "array-slice-set-constant.rkt")
    (array-slice-set-constant! B (list (:: 1 3) (:: 1 3) (:: 1 3)) '(y y y))
    B
    

    输出是:

    Welcome to DrRacket, version 6.12 [3m].
    Language: racket, with debugging.
    (mutable-array #[0 1 2 3 4 5 6 7 8 9])
    (:: 4 7 1)
    (mutable-array #[0 1 2 3 0 0 0 7 8 9])
    (mutable-array #[0 1 2 3 1 1 1 7 8 9])
    (mutable-array
     #[#[#['(0 0 0) '(0 0 1) '(0 0 2) '(0 0 3)]
         #['(0 1 0) '(0 1 1) '(0 1 2) '(0 1 3)]
         #['(0 2 0) '(0 2 1) '(0 2 2) '(0 2 3)]
         #['(0 3 0) '(0 3 1) '(0 3 2) '(0 3 3)]]
       #[#['(1 0 0) '(1 0 1) '(1 0 2) '(1 0 3)]
         #['(1 1 0) '(1 1 1) '(1 1 2) '(1 1 3)]
         #['(1 2 0) '(1 2 1) '(1 2 2) '(1 2 3)]
         #['(1 3 0) '(1 3 1) '(1 3 2) '(1 3 3)]]
       #[#['(2 0 0) '(2 0 1) '(2 0 2) '(2 0 3)]
         #['(2 1 0) '(2 1 1) '(2 1 2) '(2 1 3)]
         #['(2 2 0) '(2 2 1) '(2 2 2) '(2 2 3)]
         #['(2 3 0) '(2 3 1) '(2 3 2) '(2 3 3)]]
       #[#['(3 0 0) '(3 0 1) '(3 0 2) '(3 0 3)]
         #['(3 1 0) '(3 1 1) '(3 1 2) '(3 1 3)]
         #['(3 2 0) '(3 2 1) '(3 2 2) '(3 2 3)]
         #['(3 3 0) '(3 3 1) '(3 3 2) '(3 3 3)]]])
    (mutable-array
     #[#[#['(0 0 0) '(0 0 1) '(0 0 2) '(0 0 3)]
         #['(0 1 0) '(0 1 1) '(0 1 2) '(0 1 3)]
         #['(0 2 0) '(0 2 1) '(0 2 2) '(0 2 3)]
         #['(0 3 0) '(0 3 1) '(0 3 2) '(0 3 3)]]
       #[#['(1 0 0) '(1 0 1) '(1 0 2) '(1 0 3)]
         #['(1 1 0) '(x x x) '(x x x) '(1 1 3)]
         #['(1 2 0) '(x x x) '(x x x) '(1 2 3)]
         #['(1 3 0) '(1 3 1) '(1 3 2) '(1 3 3)]]
       #[#['(2 0 0) '(2 0 1) '(2 0 2) '(2 0 3)]
         #['(2 1 0) '(x x x) '(x x x) '(2 1 3)]
         #['(2 2 0) '(x x x) '(x x x) '(2 2 3)]
         #['(2 3 0) '(2 3 1) '(2 3 2) '(2 3 3)]]
       #[#['(3 0 0) '(3 0 1) '(3 0 2) '(3 0 3)]
         #['(3 1 0) '(3 1 1) '(3 1 2) '(3 1 3)]
         #['(3 2 0) '(3 2 1) '(3 2 2) '(3 2 3)]
         #['(3 3 0) '(3 3 1) '(3 3 2) '(3 3 3)]]])
    (mutable-array
     #[#[#['(0 0 0) '(0 0 1) '(0 0 2) '(0 0 3)]
         #['(0 1 0) '(0 1 1) '(0 1 2) '(0 1 3)]
         #['(0 2 0) '(0 2 1) '(0 2 2) '(0 2 3)]
         #['(0 3 0) '(0 3 1) '(0 3 2) '(0 3 3)]]
       #[#['(1 0 0) '(1 0 1) '(1 0 2) '(1 0 3)]
         #['(1 1 0) '(y y y) '(y y y) '(1 1 3)]
         #['(1 2 0) '(y y y) '(y y y) '(1 2 3)]
         #['(1 3 0) '(1 3 1) '(1 3 2) '(1 3 3)]]
       #[#['(2 0 0) '(2 0 1) '(2 0 2) '(2 0 3)]
         #['(2 1 0) '(y y y) '(y y y) '(2 1 3)]
         #['(2 2 0) '(y y y) '(y y y) '(2 2 3)]
         #['(2 3 0) '(2 3 1) '(2 3 2) '(2 3 3)]]
       #[#['(3 0 0) '(3 0 1) '(3 0 2) '(3 0 3)]
         #['(3 1 0) '(3 1 1) '(3 1 2) '(3 1 3)]
         #['(3 2 0) '(3 2 1) '(3 2 2) '(3 2 3)]
         #['(3 3 0) '(3 3 1) '(3 3 2) '(3 3 3)]]])
    

    文件“array-slice-set-constant.rkt”包含:

    #lang typed/racket/base
    (provide array-slice-set-constant!)
    
    (require math/private/array/for-each
             math/private/array/array-struct
             math/private/array/typed-array-indexing
             math/private/array/array-constructors
             math/private/array/for-each
             math/private/array/utils)
    
    (: array-indexes-set-constant! (All (A) ((Settable-Array A) (Array In-Indexes) A -> Void)))
    (define (array-indexes-set-constant! arr idxs constant)
      (define ds        (array-shape idxs))
      (define idxs-proc (unsafe-array-proc idxs))
      (for-each-array-index ds (λ (js) (array-set! arr (idxs-proc js) constant))))
    
    (: array-slice-set-constant! (All (A) ((Settable-Array A) (Listof Slice-Spec) A -> Void)))
    (define (array-slice-set-constant! arr slices val)
      (let ([idxs  (parameterize ([array-strictness #f])
                     (array-slice-ref (indexes-array (array-shape arr)) slices))])
        (array-indexes-set-constant! arr idxs val)))
    

    我们应该将该函数放入标准库中。

    【讨论】:

      【解决方案2】:

      ::appears to beprocedure defined in math/array

      使用:: 创建Slice 对象,使用::new 创建Slice-New-Axis 对象。只有一个Slice-Dots 对象,即::...

      我认为它一般不适用于 Racket 的其余部分。

      【讨论】:

        猜你喜欢
        • 2019-09-14
        • 2013-05-18
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        • 2011-01-17
        • 2016-12-23
        • 1970-01-01
        相关资源
        最近更新 更多