球拍:双冒号(::)是什么意思?

时间:2018-07-13 00:00:13

标签: python numpy lisp racket

是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]])

2 个答案:

答案 0 :(得分:0)

:: appears to beprocedure defined in math/array

  

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

我认为这通常不适用于Racket的其余部分。

答案 1 :(得分:0)

这里有切片的扩展示例:

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

虽然找不到将切片设置为常量的任何内容。 我继续执行下面这样的事情。

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

{% if product.tags contains 'Approved' %}
    Show approved task.
{% endif %}

{% if product.tags contains 'Waiting Approval' %}
    Show waiting approval task.
{% enfid % }

输出为:

#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

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

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)]]])

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