2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Fabrice Leray
4 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
14 /*------------------------------------------------------------------------*/
15 /* file: BasicAlgos.c */
16 /* desc : Several basic algorithms which can be used in several places in */
18 /*------------------------------------------------------------------------*/
20 #include "BasicAlgos.h"
22 #include "core_math.h"
23 #include "freeArrayOfString.h"
24 /*------------------------------------------------------------------------*/
25 double sciFindStPosMin(const double x[], int n)
37 for (i = 1 ; i < n ; i++)
39 if (x[i] > 0.0 && x[i] < min)
45 /* if we have found at least one positive value in x, min strictly positive */
48 /*------------------------------------------------------------------------*/
49 int checkMonotony(const double vector[], int nbElement)
52 if ( vector[1] >= vector[0] )
54 /* might be increasing */
55 for ( i = 1 ; i < nbElement - 1 ; i++ )
57 if ( vector[i + 1] < vector[i] )
67 /* might be decreasing */
68 for ( i = 1 ; i < nbElement - 1 ; i++ )
70 if ( vector[i + 1] > vector[i] )
82 /*------------------------------------------------------------------------*/
83 BOOL containsOneFiniteElement(const double vector[], int nbElement)
86 for (i = 0; i < nbElement; i++)
88 if (finite(vector[i]))
95 /*------------------------------------------------------------------------*/
96 void doubleArrayCopy(double dest[], const double src[], int nbElement)
98 memcpy( dest, src, nbElement * sizeof(double) ) ;
100 /*------------------------------------------------------------------------*/
101 void intArrayCopy(int dest[], const int src[], int nbElement)
103 memcpy( dest, src, nbElement * sizeof(int) ) ;
105 /*------------------------------------------------------------------------*/
106 void stringArrayCopy(char * dest[], char * src[], int nbElement)
109 for ( i = 0 ; i < nbElement ; i++ )
111 int elemSize = (int)strlen( src[i] ) + 1 ;
114 dest[i] = MALLOC( elemSize * sizeof(char) ) ;
116 if ( dest[i] == NULL )
118 destroyStringArray( dest, nbElement ) ;
122 strcpy( dest[i], src[i] ) ;
125 /*------------------------------------------------------------------------*/
126 void setDoubleArraySingleValue(double dest[], double value, int nbElement)
129 for ( i = 0 ; i < nbElement ; i++ )
134 /*------------------------------------------------------------------------*/
135 double* createNewArrayFromSource(int destSize, const double src[], int srcSize)
138 int endCopy = Min( destSize, srcSize ) ;
139 /* create new array */
140 double * dest = MALLOC( destSize * sizeof(double) ) ;
147 /* copy the element which needs to be copied */
148 memcpy( dest, src, endCopy * sizeof( double ) ) ;
150 for ( i = endCopy ; i < destSize ; i++ )
158 /*------------------------------------------------------------------------*/
159 void destroyStringArray(char * src[], int nbStrings)
161 freeArrayOfString(src, nbStrings);
163 /*--------------------------------------------------------------------------*/
164 double* createDoubleArrayCopy(const double src[], int nbElement)
166 double * res = MALLOC( nbElement * sizeof(double) ) ;
173 memcpy( res, src, nbElement * sizeof(double) ) ;
177 /*--------------------------------------------------------------------------*/
178 int * createIntArrayCopy(const int src[], int nbElement)
180 int * res = MALLOC( nbElement * sizeof(int) ) ;
187 memcpy( res, src, nbElement * sizeof(int) ) ;
191 /*--------------------------------------------------------------------------*/
192 char ** createStringArrayCopy(char * src[], int nbElement)
194 char ** res = createStringArray(nbElement);
200 stringArrayCopy( res, src, nbElement ) ;
205 /*--------------------------------------------------------------------------*/
206 char ** createStringArray(int nbElement)
209 char ** res = MALLOC( nbElement * sizeof(char *) ) ;
216 for ( i = 0 ; i < nbElement; i++ )
223 /*--------------------------------------------------------------------------*/