|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# sramp |
| 22 | + |
| 23 | +> Evaluate the [ramp function][@stdlib/math/base/special/rampf] for each element in a single-precision floating-point strided array. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var sramp = require( '@stdlib/math/strided/special/sramp' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### sramp( N, x, strideX, y, strideY ) |
| 40 | + |
| 41 | +Evaluates the [ramp function][@stdlib/math/base/special/rampf] for each element in a single-precision floating-point strided array `x` and assigns the results to elements in a single-precision floating-point strided array `y`. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 45 | + |
| 46 | +var x = new Float32Array( [ 1.0, 2.0, -3.0, 4.0, -5.0 ] ); |
| 47 | + |
| 48 | +// Perform operation in in-place: |
| 49 | +sramp( x.length, x, 1, x, 1 ); |
| 50 | +// x => <Float32Array>[ 1.0, 2.0, 0.0, 4.0, 0.0 ] |
| 51 | +``` |
| 52 | + |
| 53 | +The function accepts the following arguments: |
| 54 | + |
| 55 | +- **N**: number of indexed elements. |
| 56 | +- **x**: input [`Float32Array`][@stdlib/array/float32]. |
| 57 | +- **strideX**: index increment for `x`. |
| 58 | +- **y**: output [`Float32Array`][@stdlib/array/float32]. |
| 59 | +- **strideY**: index increment for `y`. |
| 60 | + |
| 61 | +The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to index every other value in `x` and to index the first `N` elements of `y` in reverse order, |
| 62 | + |
| 63 | +```javascript |
| 64 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 65 | + |
| 66 | +var x = new Float32Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); |
| 67 | +var y = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 68 | + |
| 69 | +sramp( 3, x, 2, y, -1 ); |
| 70 | +// y => <Float32Array>[ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 ] |
| 71 | +``` |
| 72 | + |
| 73 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][@stdlib/array/float32] views. |
| 74 | + |
| 75 | +```javascript |
| 76 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 77 | + |
| 78 | +// Initial arrays... |
| 79 | +var x0 = new Float32Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); |
| 80 | +var y0 = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 81 | + |
| 82 | +// Create offset views... |
| 83 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 84 | +var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element |
| 85 | + |
| 86 | +sramp( 3, x1, -2, y1, 1 ); |
| 87 | +// y0 => <Float32Array>[ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] |
| 88 | +``` |
| 89 | + |
| 90 | +#### sramp.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) |
| 91 | + |
| 92 | +Evaluates the [ramp function][@stdlib/math/base/special/rampf] for each element in a single-precision floating-point strided array `x` and assigns the results to elements in a single-precision floating-point strided array `y` using alternative indexing semantics. |
| 93 | + |
| 94 | +```javascript |
| 95 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 96 | + |
| 97 | +var x = new Float32Array( [ 1.0, 2.0, -3.0, 4.0, -5.0 ] ); |
| 98 | +var y = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 99 | + |
| 100 | +sramp.ndarray( x.length, x, 1, 0, y, 1, 0 ); |
| 101 | +// y => <Float32Array>[ 1.0, 2.0, 0.0, 4.0, 0.0 ] |
| 102 | +``` |
| 103 | + |
| 104 | +The function accepts the following additional arguments: |
| 105 | + |
| 106 | +- **offsetX**: starting index for `x`. |
| 107 | +- **offsetY**: starting index for `y`. |
| 108 | + |
| 109 | +While [`typed array`][@stdlib/array/float32] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to index every other value in `x` starting from the second value and to index the last `N` elements in `y`, |
| 110 | + |
| 111 | +```javascript |
| 112 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 113 | + |
| 114 | +var x = new Float32Array( [ 1.0, 2.0, -3.0, 4.0, -5.0, 6.0 ] ); |
| 115 | +var y = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); |
| 116 | + |
| 117 | +sramp.ndarray( 3, x, 2, 1, y, -1, y.length-1 ); |
| 118 | +// y => <Float32Array>[ 0.0, 0.0, 0.0, 6.0, 4.0, 2.0 ] |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.usage --> |
| 124 | + |
| 125 | +<section class="notes"> |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.notes --> |
| 130 | + |
| 131 | +<section class="examples"> |
| 132 | + |
| 133 | +## Examples |
| 134 | + |
| 135 | +<!-- eslint no-undef: "error" --> |
| 136 | + |
| 137 | +```javascript |
| 138 | +var uniform = require( '@stdlib/random/base/uniform' ); |
| 139 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 140 | +var sramp = require( '@stdlib/math/strided/special/sramp' ); |
| 141 | + |
| 142 | +var x = new Float32Array( 10 ); |
| 143 | +var y = new Float32Array( 10 ); |
| 144 | + |
| 145 | +var i; |
| 146 | +for ( i = 0; i < x.length; i++ ) { |
| 147 | + x[ i ] = uniform( -10.0, 10.0 ); |
| 148 | +} |
| 149 | +console.log( x ); |
| 150 | +console.log( y ); |
| 151 | + |
| 152 | +sramp.ndarray( x.length, x, 1, 0, y, -1, y.length-1 ); |
| 153 | +console.log( y ); |
| 154 | +``` |
| 155 | + |
| 156 | +</section> |
| 157 | + |
| 158 | +<!-- /.examples --> |
| 159 | + |
| 160 | +<!-- C interface documentation. --> |
| 161 | + |
| 162 | +* * * |
| 163 | + |
| 164 | +<section class="c"> |
| 165 | + |
| 166 | +## C APIs |
| 167 | + |
| 168 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 169 | + |
| 170 | +<section class="intro"> |
| 171 | + |
| 172 | +</section> |
| 173 | + |
| 174 | +<!-- /.intro --> |
| 175 | + |
| 176 | +<!-- C usage documentation. --> |
| 177 | + |
| 178 | +<section class="usage"> |
| 179 | + |
| 180 | +### Usage |
| 181 | + |
| 182 | +```c |
| 183 | +#include "stdlib/math/strided/special/sramp.h" |
| 184 | +``` |
| 185 | + |
| 186 | +#### stdlib_strided_sramp( N, \*X, strideX, \*Y, strideY ) |
| 187 | + |
| 188 | +Evaluates the ramp function for each element in a single-precision floating-point strided array `X` and assigns the results to elements in a single-precision floating-point strided array `Y`. |
| 189 | + |
| 190 | +```c |
| 191 | +float X[] = { 1.0, 2.0, -3.0, 4.0, -5.0, 6.0, -7.0, 8.0 }; |
| 192 | +float Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
| 193 | + |
| 194 | +int64_t N = 4; |
| 195 | + |
| 196 | +stdlib_strided_sramp( N, X, 2, Y, 2 ); |
| 197 | +``` |
| 198 | +
|
| 199 | +The function accepts the following arguments: |
| 200 | +
|
| 201 | +- **N**: `[in] int64_t` number of indexed elements. |
| 202 | +- **X**: `[in] float*` input array. |
| 203 | +- **strideX** `[in] int64_t` index increment for `X`. |
| 204 | +- **Y**: `[out] float*` output array. |
| 205 | +- **strideY**: `[in] int64_t` index increment for `Y`. |
| 206 | +
|
| 207 | +```c |
| 208 | +void stdlib_strided_sramp( const int64_t N, const float *X, const int64_t strideX, float *Y, const int64_t strideY ); |
| 209 | +``` |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.usage --> |
| 214 | + |
| 215 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 216 | + |
| 217 | +<section class="notes"> |
| 218 | + |
| 219 | +</section> |
| 220 | + |
| 221 | +<!-- /.notes --> |
| 222 | + |
| 223 | +<!-- C API usage examples. --> |
| 224 | + |
| 225 | +<section class="examples"> |
| 226 | + |
| 227 | +### Examples |
| 228 | + |
| 229 | +```c |
| 230 | +#include "stdlib/math/strided/special/sramp.h" |
| 231 | +#include <stdint.h> |
| 232 | +#include <stdio.h> |
| 233 | + |
| 234 | +int main() { |
| 235 | + // Create an input strided array: |
| 236 | + float X[] = { 1.0, 2.0, -3.0, 4.0, -5.0, 6.0, -7.0, 8.2 }; |
| 237 | + |
| 238 | + // Create an output strided array: |
| 239 | + float Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
| 240 | + |
| 241 | + // Specify the number of elements: |
| 242 | + int64_t N = 4; |
| 243 | + |
| 244 | + // Specify the stride lengths: |
| 245 | + int64_t strideX = 2; |
| 246 | + int64_t strideY = 2; |
| 247 | + |
| 248 | + // Compute the results: |
| 249 | + stdlib_strided_sramp( N, X, strideX, Y, strideY ); |
| 250 | + |
| 251 | + // Print the results: |
| 252 | + for ( int i = 0; i < 8; i++ ) { |
| 253 | + printf( "Y[ %i ] = %f\n", i, Y[ i ] ); |
| 254 | + } |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +</section> |
| 259 | + |
| 260 | +<!-- /.examples --> |
| 261 | + |
| 262 | +</section> |
| 263 | + |
| 264 | +<!-- /.c --> |
| 265 | + |
| 266 | +<section class="links"> |
| 267 | + |
| 268 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib |
| 269 | + |
| 270 | +[@stdlib/math/base/special/rampf]: https://github.com/stdlib-js/stdlib |
| 271 | + |
| 272 | +</section> |
| 273 | + |
| 274 | +<!-- /.links --> |
0 commit comments