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)
35 for (i = 0; i < n; i++)
46 if (x[i] > 0.0 && x[i] < min)
52 /* if we have found at least one positive value in x, min strictly positive */
55 /*------------------------------------------------------------------------*/
56 int checkMonotony(const double vector[], int nbElement)
59 if ( vector[1] >= vector[0] )
61 /* might be increasing */
62 for ( i = 1 ; i < nbElement - 1 ; i++ )
64 if ( vector[i + 1] < vector[i] )
74 /* might be decreasing */
75 for ( i = 1 ; i < nbElement - 1 ; i++ )
77 if ( vector[i + 1] > vector[i] )
89 /*------------------------------------------------------------------------*/
90 BOOL containsOneFiniteElement(const double vector[], int nbElement)
93 for (i = 0; i < nbElement; i++)
95 if (finite(vector[i]))
102 /*------------------------------------------------------------------------*/
103 void doubleArrayCopy(double dest[], const double src[], int nbElement)
105 memcpy( dest, src, nbElement * sizeof(double) ) ;
107 /*------------------------------------------------------------------------*/
108 void intArrayCopy(int dest[], const int src[], int nbElement)
110 memcpy( dest, src, nbElement * sizeof(int) ) ;
112 /*------------------------------------------------------------------------*/
113 void stringArrayCopy(char * dest[], char * src[], int nbElement)
116 for ( i = 0 ; i < nbElement ; i++ )
118 int elemSize = (int)strlen( src[i] ) + 1 ;
121 dest[i] = MALLOC( elemSize * sizeof(char) ) ;
123 if ( dest[i] == NULL )
125 destroyStringArray( dest, nbElement ) ;
129 strcpy( dest[i], src[i] ) ;
132 /*------------------------------------------------------------------------*/
133 void setDoubleArraySingleValue(double dest[], double value, int nbElement)
136 for ( i = 0 ; i < nbElement ; i++ )
141 /*------------------------------------------------------------------------*/
142 double* createNewArrayFromSource(int destSize, const double src[], int srcSize)
145 int endCopy = Min( destSize, srcSize ) ;
146 /* create new array */
147 double * dest = MALLOC( destSize * sizeof(double) ) ;
154 /* copy the element which needs to be copied */
155 memcpy( dest, src, endCopy * sizeof( double ) ) ;
157 for ( i = endCopy ; i < destSize ; i++ )
165 /*------------------------------------------------------------------------*/
166 void destroyStringArray(char * src[], int nbStrings)
168 freeArrayOfString(src, nbStrings);
170 /*--------------------------------------------------------------------------*/
171 double* createDoubleArrayCopy(const double src[], int nbElement)
173 double * res = MALLOC( nbElement * sizeof(double) ) ;
180 memcpy( res, src, nbElement * sizeof(double) ) ;
184 /*--------------------------------------------------------------------------*/
185 int * createIntArrayCopy(const int src[], int nbElement)
187 int * res = MALLOC( nbElement * sizeof(int) ) ;
194 memcpy( res, src, nbElement * sizeof(int) ) ;
198 /*--------------------------------------------------------------------------*/
199 char ** createStringArrayCopy(char * src[], int nbElement)
201 char ** res = createStringArray(nbElement);
207 stringArrayCopy( res, src, nbElement ) ;
212 /*--------------------------------------------------------------------------*/
213 char ** createStringArray(int nbElement)
216 char ** res = MALLOC( nbElement * sizeof(char *) ) ;
223 for ( i = 0 ; i < nbElement; i++ )
230 /*--------------------------------------------------------------------------*/