📄 vector_int.c
字号:
/* * * QccPack: Quantization, compression, and coding libraries * Copyright (C) 1997-2009 James E. Fowler * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, * MA 02139, USA. * */#include "libQccPack.h"QccVectorInt QccVectorIntAlloc(int vector_dimension){ QccVectorInt vector; if (vector_dimension <= 0) return(NULL); if ((vector = (QccVectorInt)calloc(vector_dimension, sizeof(int))) == NULL) QccErrorAddMessage("(QccVectorIntAlloc): Error allocating memory"); return(vector);}void QccVectorIntFree(QccVectorInt vector){ if (vector != NULL) QccFree(vector);}int QccVectorIntZero(QccVectorInt vector, int vector_dimension){ int component; if ((vector == NULL) || (vector_dimension <= 0)) return(0); for (component = 0; component < vector_dimension; component++) vector[component] = 0; return(0);}QccVectorInt QccVectorIntResize(QccVectorInt vector, int vector_dimension, int new_vector_dimension){ int index; QccVectorInt new_vector = NULL; if ((vector_dimension < 0) || (new_vector_dimension < 0)) return(vector); if (new_vector_dimension == vector_dimension) return(vector); if (vector == NULL) { vector_dimension = 0; if ((new_vector = QccVectorIntAlloc(new_vector_dimension)) == NULL) { QccErrorAddMessage("(QccVectorIntResize): Error calling QccVectorIntAlloc()"); return(NULL); } } else { if ((new_vector = (QccVectorInt)realloc((void *)vector, sizeof(int) * new_vector_dimension)) == NULL) { QccErrorAddMessage("(QccVectorIntRealloc): Error reallocating memory"); return(NULL); } } for (index = vector_dimension; index < new_vector_dimension; index++) new_vector[index] = 0; return(new_vector);}double QccVectorIntMean(const QccVectorInt vector, int vector_dimension){ double mean = 0.0; int component; if ((vector == NULL) || (vector_dimension <= 0)) return((double)0.0); for (component = 0; component < vector_dimension; component++) mean += vector[component]; mean /= vector_dimension; return(mean);}double QccVectorIntVariance(const QccVectorInt vector, int vector_dimension){ double mean; double variance = 0; int component; if ((vector == NULL) || (vector_dimension <= 0)) return((double)0.0); mean = QccVectorIntMean(vector, vector_dimension); for (component = 0; component < vector_dimension; component++) variance += (vector[component] - mean) * (vector[component] - mean); variance /= vector_dimension; return(variance);}int QccVectorIntAdd(QccVectorInt vector1, const QccVectorInt vector2, int vector_dimension){ int component; if ((vector1 == NULL) || (vector2 == NULL) || (vector_dimension <= 0)) return(0); for (component = 0; component < vector_dimension; component++) vector1[component] += vector2[component]; return(0);}int QccVectorIntSubtract(QccVectorInt vector1, const QccVectorInt vector2, int vector_dimension){ int component; if ((vector1 == NULL) || (vector2 == NULL) || (vector_dimension <= 0)) return(0); for (component = 0; component < vector_dimension; component++) vector1[component] -= vector2[component]; return(0);}int QccVectorIntScalarMult(QccVectorInt vector, int s, int vector_dimension){ int component; if ((vector == NULL) || (vector_dimension <= 0)) return(0); for (component = 0; component < vector_dimension; component++) vector[component] *= s; return(0);}int QccVectorIntCopy(QccVectorInt vector1, const QccVectorInt vector2, int vector_dimension){ int component; if ((vector1 == NULL) || (vector2 == NULL) || (vector_dimension <= 0)) return(0); for (component = 0; component < vector_dimension; component++) vector1[component] = vector2[component]; return(0);}double QccVectorIntNorm(const QccVectorInt vector, int vector_dimension){ int component; double squared_norm = 0.0; if ((vector == NULL) || (vector_dimension <= 0)) return(squared_norm); for (component = 0; component < vector_dimension; component++) squared_norm += vector[component] * vector[component]; return(sqrt(squared_norm));}int QccVectorIntDotProduct(const QccVectorInt vector1, const QccVectorInt vector2, int vector_dimension){ int component; int dot_product = 0; if ((vector1 == NULL) || (vector2 == NULL) || (vector_dimension <= 0)) return(0); for (component = 0; component < vector_dimension; component++) dot_product += vector1[component] * vector2[component]; return(dot_product);}int QccVectorIntSquareDistance(const QccVectorInt vector1, const QccVectorInt vector2, int vector_dimension){ int diff, distance = 0; int component; if ((vector1 == NULL) || (vector2 == NULL) || (vector_dimension <= 0)) return((int)0); for (component = 0; component < vector_dimension; component++) { diff = vector1[component] - vector2[component]; distance += diff * diff; } return(distance);}int QccVectorIntSumComponents(const QccVectorInt vector, int vector_dimension){ int component; int sum = 0; if (vector == NULL) return(0); for (component = 0; component < vector_dimension; component++) sum += vector[component]; return(sum);}int QccVectorIntMaxValue(const QccVectorInt vector, int vector_dimension, int *winner){ int component; int max_value = -MAXINT; int max_index = 0; if (vector != NULL) for (component = 0; component < vector_dimension; component++) if (vector[component] > max_value) { max_value = vector[component]; max_index = component; } if (winner != NULL) *winner = max_index; return(max_value);}int QccVectorIntMinValue(const QccVectorInt vector, int vector_dimension, int *winner){ int component; int min_value = MAXINT; int min_index = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -