main.c (1938B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "stdlib/strided/base/dmap.h" 20 #include <stdint.h> 21 22 /** 23 * Applies a unary function accepting and returning double-precision floating-point numbers to each element in a double-precision floating-point strided input array and assigns each result to an element in a double-precision floating-point strided output array. 24 * 25 * @param N number of indexed elements 26 * @param X input array 27 * @param strideX X stride length 28 * @param Y destination array 29 * @param strideY Y stride length 30 * @param fcn unary function to apply 31 * 32 * @example 33 * #include "stdlib/strided/base/dmap.h" 34 * #include <stdint.h> 35 * 36 * static double scale( const double x ) { 37 * return x * 10.0; 38 * } 39 * 40 * double X[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; 41 * double Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; 42 * 43 * int64_t N = 6; 44 * 45 * stdlib_strided_dmap( N, X, 1, Y, 1, scale ); 46 */ 47 void stdlib_strided_dmap( const int64_t N, const double *X, const int64_t strideX, double *Y, const int64_t strideY, double (*fcn)( double ) ) { 48 int64_t ix; 49 int64_t iy; 50 int64_t i; 51 if ( N <= 0 ) { 52 return; 53 } 54 if ( strideX < 0 ) { 55 ix = (1-N) * strideX; 56 } else { 57 ix = 0; 58 } 59 if ( strideY < 0 ) { 60 iy = (1-N) * strideY; 61 } else { 62 iy = 0; 63 } 64 for ( i = 0; i < N; i++ ) { 65 Y[ iy ] = fcn( X[ ix ] ); 66 ix += strideX; 67 iy += strideY; 68 } 69 return; 70 }