void VecArrayresize(const VecArray a, int newsize) { int i, j, m, nr, nc; Vector V; g_assert(a); nc = VecArraySize(a); nr = VecArrayVectorSize(a); g_assert(nr); g_assert(nc); if(newsize < nr) m = newsize; else m = nr; for(i = 0; i < nc; i++) { V = newVector(newsize); for(j = 0; j < m; j++) V[j] = a[i][j]; if(newsize > nr) for(j = nr; j < newsize; j++) a[i][j] = 0.0; deleteVector(a[i]); a[i] = V; } }
static inline Statement *newStatement() { Statement *statement = malloc(sizeof(Statement)); statement->next = NULL; statement->code = newVector(25); return statement; }
/*! * Load shape level connectors * \param ppFileData Pointer to the data (usualy read from a file) * \param s Pointer to shape level * \return false on error (memory allocation failure/bad file format), true otherwise * \pre ppFileData loaded * \pre s allocated * \pre s->nconnectors set * \post s->connectors allocated */ static bool _imd_load_connectors(const char **ppFileData, iIMDShape *s) { const char *pFileData = *ppFileData; int cnt; Vector3i *p = NULL, newVector(0, 0, 0); s->connectors = (Vector3i *)malloc(sizeof(Vector3i) * s->nconnectors); if (s->connectors == NULL) { debug(LOG_ERROR, "(_load_connectors) MALLOC fail"); return false; } for (p = s->connectors; p < s->connectors + s->nconnectors; p++) { if (sscanf(pFileData, "%d %d %d%n", &newVector.x, &newVector.y, &newVector.z, &cnt) != 3 && sscanf(pFileData, "%d%*[.0-9] %d%*[.0-9] %d%*[.0-9]%n", &newVector.x, &newVector.y, &newVector.z, &cnt) != 3) { debug(LOG_ERROR, "(_load_connectors) file corrupt -M"); return false; } pFileData += cnt; *p = newVector; } *ppFileData = pFileData; return true; }
Scope *newScope(Scope *enclosingScope) { debug(E_DEBUG, "Creating new scope\n"); Scope *scope = malloc(sizeof(Scope)); scope->enclosingScope = enclosingScope; scope->variables = newVector(25); return scope; }
GPolygon newGPolygon(void) { GPolygon poly = newGObject(GPOLYGON); poly->u.polygonRep.vertices = newVector(); poly->u.polygonRep.cx = 0; poly->u.polygonRep.cy = 0; createGPolygonOp(poly); return poly; }
LispRef content(LispRef stream, LispRef eos_error_p, LispRef eos_value) { WITH_DEBUG(fprintf(stderr, "content\n")); char tag; read_byte(tag); WITH_DEBUG(fprintf(stderr, " tag: %x\n", tag)); switch (tag) { case TC_NULL: return nullReference(stream, eos_error_p, eos_value); case TC_REFERENCE: return prevObject(stream, eos_error_p, eos_value); case TC_CLASS: return newClass(stream, eos_error_p, eos_value); case TC_OBJECT: return newObject(stream, eos_error_p, eos_value); case TC_STRING: return newString(stream, eos_error_p, eos_value); case TC_STATE: return newState(stream, eos_error_p, eos_value); case TC_VECTOR: return newVector(stream, eos_error_p, eos_value); case TC_STREAM: return newStream(stream, eos_error_p, eos_value); case TC_RESET: return reset(stream, eos_error_p, eos_value); case TC_SELF: return stream; case TC_FUNCTION: return newFunction(stream, eos_error_p, eos_value); case TC_BYTEVECTOR: return newBytevector(stream, eos_error_p, eos_value); case TC_INT: return newInt(stream, eos_error_p, eos_value); case TC_DOUBLE: return newDouble(stream, eos_error_p, eos_value); case TC_SYMBOL: return newSymbol(stream, eos_error_p, eos_value); case TC_KEYWORD: return newKeyword(stream, eos_error_p, eos_value); case TC_CHAR: return newChar(stream, eos_error_p, eos_value); case TC_CONS: return newCons(stream, eos_error_p, eos_value); default: { LispRef str, args; eul_allocate_string(str, "unknown tag in ~a"); eul_allocate_cons(args, stream, eul_nil); eul_serial_error(stream, str, args); return eul_nil; } } }
///--------------------------------------------------------------------------------- /// slow ///--------------------------------------------------------------------------------- inline const Vector4< int > Vector4< int >::operator /( int inverseScale ) const { if( inverseScale == 0 ) return Vector4< int >( 0, 0, 0, 0 ); Vector4< int > newVector( x / inverseScale, y / inverseScale, z / inverseScale, w / inverseScale ); return newVector; }
void main(int argc, char* argv[]) { realVector* v = newVector(); vector_append(v, 1.0); vector_append(v, 2.0); printf("v[0]=%f\n", vector_get(v, 0)); printf("v[0]=%f\n", vector_get(v,1)); }
continuation newContinuation(void *origin, obj selector, vector evaluated, vector unevaluated, vector scope, vector env, vector old) { return newVector(8, origin, selector, evaluated, unevaluated, scope, env, NULL, old); }
// now main program, as usual, with some options parsing, etc. int main(int argc, char** argv){ int i; pTimer T = newTimer(); startTimer(T); // let's measure execution time of the whole program. // options parsing, configuration and reporting variables parseOptions(argc,argv); printf("[i] vector size is: %li\n",SIZE); printf("[i] threads requested: %i\n", PTHR); printf("[i] total mem for data: %1.2f MB\n",SIZE*sizeof(Data)/1024.0/1024.0); if(PTHR<=0){printf("Too few threads, exiting...\n");exit(0);} printf("\n"); // preparing data for calculations pVector V = newVector(SIZE); setVector(V,1.0); // preparing barrier for all threads involved in calculations if(pthread_barrier_init( &barrier, NULL, PTHR)){ fprintf(stderr,"[E] could not create barrier!\n"); exit(-1); } // preparing threads for calculations Info threads[PTHR]; for(i=0;i<PTHR;i++){ threads[i].nr = i; threads[i].V = V; threads[i].sum = 0.0; threads[i].start = (i )*SIZE/PTHR; threads[i].end = (i+1)*SIZE/PTHR; int rc = pthread_create(&threads[i].id,NULL,reduceVector,(void*) &threads[i]); if(rc){ fprintf(stderr,"[E] thread creation error, code returned: %i\n",rc); exit(-1); } else{ if(LOUD) printf("[i] thread %i: start=%i, end=%i\n",i,threads[i].start,threads[i].end); } } for(i=0;i<PTHR;i++){ if(pthread_join(threads[i].id,NULL)){ fprintf(stderr,"[e] Can't join thread id=%li, nr=%i\n",threads[i].id,threads[i].nr); return -1; } } // at the end we have to collect all data from threads double sum = 0.0; double time = 0.0; for(i=0;i<PTHR;i++){ sum += threads[i].sum; time+= threads[i].time; } stopTimer(T); printf("Main sum is %1.2f, avg thread time: %-1.12f s, TOTAL time: %1.12f\n",sum,time/PTHR,getTime(T)); freeTimer(T); }
///--------------------------------------------------------------------------------- /// slow ///--------------------------------------------------------------------------------- inline const Vector4< float > Vector4< float >::operator /( float inverseScale ) const { if( inverseScale == 0.0f ) return Vector4f( 0.0f, 0.0f, 0.0f, 0.0f ); float scale = 1.0f / inverseScale; Vector4f newVector( x * scale, y * scale, z * scale, w * scale ); return newVector; }
std::vector<char> LargeGapGrayCode::multiCopyVector(const std::vector<char>& oldVector, int numCopies) { std::vector<char> newVector(oldVector.size()*numCopies); for(int i = 0; i < numCopies; ++i) { for(int j = 0; j < oldVector.size(); ++j) { newVector[i*oldVector.size() + j] = oldVector[j]; } } return newVector; }
///--------------------------------------------------------------------------------- /// slow ///--------------------------------------------------------------------------------- inline const Vector4< double > Vector4< double >::operator /( double inverseScale ) const { if( inverseScale == 0.0 ) return Vector4d( 0.0, 0.0, 0.0, 0.0 ); double scale = 1.0 / inverseScale; Vector4d newVector( x * scale, y * scale, z * scale, w * scale ); return newVector; }
///--------------------------------------------------------------------------------- /// slow ///--------------------------------------------------------------------------------- inline const Vector2 Vector2::operator /( float inverseScale ) const { if( inverseScale == 0.0f ) return Vector2( 0.0f, 0.0f ); float scale = 1.0f / inverseScale; Vector2 newVector( x * scale, y * scale ); return newVector; }
static Expression *newExpression() { Expression *expr = malloc(sizeof(Expression)); expr->type = CONST; expr->next = NULL; expr->inferredType = UNKNOWN; expr->variableExpression = NULL; expr->place = NULL; expr->code = newVector(25); return expr; }
Suitors::Suitors( unsigned int initial, unsigned int letters) : letters(letters) { // declare the vector to the given size and populate it with ints counting from 1 vector<unsigned int> newVector(initial); suitorList = newVector; for (current = 0; current < suitorList.size( ); current++) { suitorList[current] = current + 1; } // reset current for the first elimination current = 0; }
Status HashIndexCursor::seek(const BSONObj& position) { //Use FieldRangeSet to parse the query into a vector of intervals //These should be point-intervals if this cursor is ever used //So the FieldInterval vector will be, e.g. <[1,1], [3,3], [6,6]> FieldRangeSet frs( "" , position, true, true ); const vector<FieldInterval>& intervals = frs.range( _hashedField.c_str() ).intervals(); //Construct a new query based on the hashes of the previous point-intervals //e.g. {a : {$in : [ hash(1) , hash(3) , hash(6) ]}} BSONObjBuilder newQueryBuilder; BSONObjBuilder inObj( newQueryBuilder.subobjStart( _hashedField ) ); BSONArrayBuilder inArray( inObj.subarrayStart("$in") ); vector<FieldInterval>::const_iterator i; for( i = intervals.begin(); i != intervals.end(); ++i ){ if ( ! i->equality() ){ _oldCursor.reset( BtreeCursor::make( nsdetails( _descriptor->parentNS()), _descriptor->getOnDisk(), BSON( "" << MINKEY ) , BSON( "" << MAXKEY ) , true , 1 ) ); return Status::OK(); } inArray.append(HashAccessMethod::makeSingleKey(i->_lower._bound, _seed, _hashVersion)); } inArray.done(); inObj.done(); BSONObj newQuery = newQueryBuilder.obj(); // FieldRangeVector needs an IndexSpec so we make it one. BSONObjBuilder specBuilder; BSONObjIterator it(_descriptor->keyPattern()); while (it.more()) { BSONElement e = it.next(); specBuilder.append(e.fieldName(), 1); } BSONObj spec = specBuilder.obj(); IndexSpec specForFRV(spec); //Use the point-intervals of the new query to create a Btree cursor FieldRangeSet newfrs( "" , newQuery , true, true ); shared_ptr<FieldRangeVector> newVector( new FieldRangeVector( newfrs , specForFRV, 1 ) ); _oldCursor.reset( BtreeCursor::make(nsdetails(_descriptor->parentNS()), _descriptor->getOnDisk(), newVector, 0, 1)); return Status::OK(); }
VecArray VecArrayfromfile(const char *filename, int mincols) { VecArray a; Vector v; FILE *in; int nc = 0, nr = 0, n, i, row; char line[1024]; in = fopen(filename, "r"); if(!in) return 0; for(;;) { fgets(line, 1023, in); if(feof(in)) break; /* look for "standard" comment characters */ if(line[0] == '#' || line[0] > 57 || line[0] == '*' || line[0] == '!' || line[0] == ';') continue; n = stringtoVector(line, 0); if(n >= mincols) { if(nc == 0) nc = n; if(n >= nc) nr++; } } fclose(in); a = newpopulatedVecArray(nc, nr); v = newVector(nc); in = fopen(filename, "r"); for(row = 0; row < nr;) { fgets(line, 1023, in); if(feof(in)) break; if(line[0] == '#' || line[0] > 57 || line[0] == '*') continue; n = stringtoVector(line, v); if(n >= nc) { if(n >= nc) { for(i = 0; i < nc; i++) a[i][row] = v[i]; row++; } } } fclose(in); deleteVector(v); return a; }
void init() { srand(time(NULL)); numUnitCircleVertices = 64; unitCircle = generateUnitCircle(numUnitCircleVertices); b = (Body *)malloc(bodies*sizeof(Body)); int i; for(i=0;i<bodies;++i) { b[i] = newBody( newVector( 1-2*(float)rand()/(float)RAND_MAX, 1-2*(float)rand()/(float)RAND_MAX ), newVector( 0.002-0.004*(float)rand()/(float)RAND_MAX, 0.002-0.004*(float)rand()/(float)RAND_MAX ), 100*(float)rand()/(float)RAND_MAX ); } b[0] = newBody(newVector(0,0.0001),newVector(0,0),2000); }
shared_ptr<Cursor> HashedIndexType::newCursor( const BSONObj& query , const BSONObj& order , int numWanted ) const { //Use FieldRangeSet to parse the query into a vector of intervals //These should be point-intervals if this cursor is ever used //So the FieldInterval vector will be, e.g. <[1,1], [3,3], [6,6]> FieldRangeSet frs( "" , query , true, true ); const vector<FieldInterval>& intervals = frs.range( _hashedField.c_str() ).intervals(); //Force a match of the query against the actual document by giving //the cursor a matcher with an empty indexKeyPattern. This insures the //index is not used as a covered index. //NOTE: this forcing is necessary due to potential hash collisions const shared_ptr< CoveredIndexMatcher > forceDocMatcher( new CoveredIndexMatcher( query , BSONObj() ) ); //Construct a new query based on the hashes of the previous point-intervals //e.g. {a : {$in : [ hash(1) , hash(3) , hash(6) ]}} BSONObjBuilder newQueryBuilder; BSONObjBuilder inObj( newQueryBuilder.subobjStart( _hashedField ) ); BSONArrayBuilder inArray( inObj.subarrayStart("$in") ); vector<FieldInterval>::const_iterator i; for( i = intervals.begin(); i != intervals.end(); ++i ){ if ( ! i->equality() ){ const shared_ptr< BtreeCursor > exhaustiveCursor( BtreeCursor::make( nsdetails( _spec->getDetails()->parentNS().c_str()), *( _spec->getDetails() ), BSON( "" << MINKEY ) , BSON( "" << MAXKEY ) , true , 1 ) ); exhaustiveCursor->setMatcher( forceDocMatcher ); return exhaustiveCursor; } inArray.append( makeSingleKey( i->_lower._bound , _seed , _hashVersion ) ); } inArray.done(); inObj.done(); BSONObj newQuery = newQueryBuilder.obj(); //Use the point-intervals of the new query to create a Btree cursor FieldRangeSet newfrs( "" , newQuery , true, true ); shared_ptr<FieldRangeVector> newVector( new FieldRangeVector( newfrs , *_spec , 1 ) ); const shared_ptr< BtreeCursor > cursor( BtreeCursor::make( nsdetails( _spec->getDetails()->parentNS().c_str()), *( _spec->getDetails() ), newVector , 1 ) ); cursor->setMatcher( forceDocMatcher ); return cursor; }
double MRO_InfinityNorm::operator()() const { ColumnVector newVector(getRows()); unsigned int i, j; for (i = 0; i < getRows(); i++) { newVector.element(i) = 0; for (j = 0; j < getColumns(); j++) { newVector.element(i) += fabs(element(i, j)); } } return newVector.maximum(); }
RowVector MRO_ColumnSum::operator()() const { RowVector newVector(getColumns()); unsigned int i, j; for (j = 0; j < getColumns(); j++) { newVector.element(j) = 0; for (i = 0; i < getRows(); i++) { newVector.element(j) += element(i, j); } } return newVector; }
nodeType* constantVec(double x, double y, double z) { nodeType *p; /* allocate node */ if((p = malloc(sizeof(constantNodeType))) == NULL) yyerror("Out of memory encountered."); assert(p); /* copy information */ p->type = typeVecConstant; p->con.vector = newVector(x, y, z); return p; }
FunctionDeclaration *newFunction(Type returnType, char *functionName, Vector *parameters, VariableDeclaration *declarations, Statement *body) { FunctionDeclaration *funcDecl = malloc(sizeof(FunctionDeclaration)); funcDecl->returnType = returnType; funcDecl->functionName = functionName; funcDecl->parameters = parameters; funcDecl->declarations = declarations; funcDecl->body = body; funcDecl->next = NULL; funcDecl->functionScope = NULL; funcDecl->code = newVector(25); return funcDecl; }
void StatusVector::ImplStatusVector::prepend(const StatusVector& v) throw() { ImplStatusVector newVector(getKind(), getCode()); if (newVector.appendErrors(v.implementation)) { if (newVector.appendErrors(this)) { if (newVector.appendWarnings(v.implementation)) newVector.appendWarnings(this); } } *this = newVector; }
int main(int argc, const char * argv[]) { /* | x -2y -2z = -1 | x -y +z = -2 | 2x +y +3z = 1 reposta: x = 1, y = 2 e z = –1. */ float **matriz = newMatrix(3,3); matriz[0][0]=12;matriz[0][1]=643;matriz[0][2]=106; matriz[1][0]=643;matriz[1][1]=34.843;matriz[1][2]= 5.779; matriz[2][0]=106;matriz[2][1]= 5.77;matriz[2][2]= 976; float *coll4 = newVector(3); coll4[0]=753; coll4[1]=40830; coll4[2]= 6796; printf("Considerando:\n"); printf("|ax +by +cz = j\n"); printf("|dx +ey +fz = k\n"); printf("|gx +hy +iz = l\n"); printf("\nTemos para:\n"); printf("|ax +by +cz|\n"); printf("|dx +ey +fz|\n"); printf("|gx +hy +iz|\n"); printf("\na matriz:\n"); printMatrix(matriz, 3, 3); printf("e para:\n"); printf("|j|\n|k|\n|l|\n"); printf("\no vetor:\n"); printVector(coll4, 3); float * resposta; // = cramer3x3(1, -2, -2, 1, -1, 1, 2, 1, 3, -1, -2, 1); resposta = cramerMatrizTamanhoVector(matriz, 3, coll4); printf("\nA resposta deve ser: x = 1, y = 2 e z = –1\n"); printf("E a resposta calculada é: x=%f, y:%f, z:%f\n\n", resposta[0], resposta[1], resposta[2]); return 0; }
VecArray newVecArrayfromMatrix(const Matrix M) { int i, j, m, n; VecArray a; g_assert(M); n = MatrixSize1(M); m = MatrixSize2(M); a = newVecArray(n); for(j = 0; j < n; j++) { a[j] = newVector(m); for(i = 0; i < m; i++) a[j][i] = M[j][i]; } return a; }
Vector VecArrayVectoraverage(const VecArray a) { int N, M, j; Vector v; g_assert(a); N = VecArrayVectorSize(a); if(N <= 0) return 0; M = VecArraySize(a); v = newVector(N); zeroVector(v); for(j = 0; j < M; j++) addtoVector(v, a[j]); scaleVector(v, 1.0/M); return v; }
Element *dupElement(Element *elm){ Element *ret = newElement(elm->type, NULL); if(ret->type == ET_STRING){ ret->data = (void*)dup((char*)elm->data); }else if(ret->type == ET_INTEGER || ret->type == ET_DECIMAL){ ret->dval = elm->dval; ret->ival = elm->ival; }else if(ret->type == ET_VECTOR){ Vector *v = (Vector*)elm->data; Vector *vnew = newVector(); for(int i=0; i<v->len; ++i){ vectorPushBack(vnew, dupElement(v->data[i])); } ret->data = (void*)vnew; } cloneOps(ret, elm); return ret; }
Vector<T> Matrix<T>::operator* (const Vector<T>& rhs) const { if (size()==0) throw std::length_error("Can not perform operations on an empty matrix."); if (size() != rhs.size()) throw std::length_error("The size of the Vector and Matrix must be the same"); Vector<T> newVector(size()); for (unsigned int i=0;i<size();i++) { T sum = 0; for (unsigned int j=0;j<size();j++) { sum += m_matrix[i][j]*rhs[j]; } newVector[i] = sum; } return newVector; }