Python 球拍:双冒号(:)是什么意思?
Racket是个新手,由于某些原因,在官方文档中找不到这个。这个Python 球拍:双冒号(:)是什么意思?,python,numpy,lisp,racket,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
(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]])
:
a:
使用创建对象和使用创建对象。只有一个对象,即
我不认为它通常适用于球拍的其他部分。这里有一个扩展的切片示例: 但找不到任何将切片设置为常量的内容。 我继续实施了这样一件事,见下文 首先,让我们尝试一些例子
#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)))
我们应该将该函数放入标准库中