这里有一个扩展的切片示例:
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)))
我们应该将该功能放入标准库中。
:: 似乎 一个 程序定义于 math/array :
::
math/array
创建 Slice 使用的对象 :: 和 Slice-New-Axis 使用的对象 ::new 。只有一个 Slice-Dots 对象,即 ::... 。
Slice
Slice-New-Axis
::new
Slice-Dots
::...
我不认为它通常适用于Racket的其他部分。