예제 #1
0
void Inversa::run(){
  //showMatriz(m);

  matriz *v;
  v = (matriz*)malloc(sizeof(matriz));
  v->rows=3;v->columns=1;
  v->data = aloca(3,1);
  v->data[0][0]=1;v->data[1][0]=-2;v->data[2][0]=2;

  backupM(); //keep m
  initInve();



  gaussInversa(m);

  normalizePivots();
  printf("\n\n");
  printf("%s\n\n","MTRIZ A DADA:" );
  showMatriz(backup);
  printf("%s\n\n","SUA INVERSA A¹ APROX:" );
  showMatriz(mInve);
  printf("DEVERIA SER I:\n");
  printf("%s\n\n","APROX: A¹ x A:" );
  savePartialResult(times(mInve,backup));
  showMatriz(buffer);
  printf("%s\n","APLICANDO POTENCIA NESSA MATRIZ APROX INVERSA:\n\n" );
  printf("vetor inicial aleatório:\n");
  showMatriz(v);
  printf("============ INICIO ===========\n");
  potenciaRegular(buffer,v,0.01);
  printf("============  FIM  ===========\n");


}
예제 #2
0
void DotlikeOptimizer::Reorder(Layout &nodes,Layout &edges) {
	vector<int> affectedRanks;
	{
		NodeV optimVec;
		getCrossoptModelNodes(nodes,edges,optimVec);
		if(optimVec.empty())
			return;
		sort(optimVec.begin(),optimVec.end(),RankLess());
		NodeV::iterator wot = optimVec.begin();
		for(Config::Ranks::iterator ri = config.ranking.begin(); ri!=config.ranking.end(); ++ri)
			for(NodeV::iterator ni = (*ri)->order.begin(); ni!=(*ri)->order.end(); ++ni)
				if(wot!=optimVec.end() && *ni==*wot) {
					DDd(*ni).orderConstraint = -1;
					++wot;
				}
				else
					DDd(*ni).orderConstraint = DDd(*ni).order;
		assert(wot==optimVec.end());
		loops.Field(r_crossopt,"model nodes for crossopt",optimVec.size());
		loops.Field(r_crossopt,"total model nodes",optimVec.front()->g->nodes().size());

		for(NodeV::iterator ni = optimVec.begin(); ni!=optimVec.end(); ++ni)
			if(affectedRanks.empty() || affectedRanks.back()!=DDd(*ni).rank)
				affectedRanks.push_back(DDd(*ni).rank);
		loops.Field(r_crossopt,"ranks for crossopt",affectedRanks.size());
		loops.Field(r_crossopt,"total ranks",config.ranking.size());
	}
	SiftMatrix matrix(config),backupM(config);
	MedianCompare median(DOWN,false);
	CrossingCompare crossing(config,matrix,false);
	OrderConstraintSwitchable switchable;

	Config::Ranks backup = config.ranking;
	// optimize once ignoring node crossings (they can scare the sifting alg) 
	// in a sec we'll sift using the node penalty to clean up
	matrix.m_light = true;
	matrix.recompute();
	backupM = matrix;
	unsigned best = matrix.sumCrossings(),bestPass=0;
	loops.Field(r_crossopt,"crossings before crossopt",best);
	unsigned passes = 32,pass=0,score=0;
	while(pass<passes && best) {
		int tired = 0;
		while(pass<passes && tired<TIRE) {
				assert(matrix.m_light);
			median.m_allowEqual = pass%8>4;
			crossing.m_allowEqual = !median.m_allowEqual;
			LeftRight way = (pass%2) ? RIGHT : LEFT;
			UpDown dir;
				assert(matrix.m_light);
			if(pass%4<2) {
				median.m_dir = UP;
				dir = DOWN;
			}
			else {
				median.m_dir = DOWN;
				dir = UP;
			}
			bubblePass(config,matrix,affectedRanks,dir,way,switchable,median);
				assert(matrix.m_light);

			// something in the next dozen lines crashes MS Visual C++ .NET
			// if run-time checks are enabled so i've disabled them for this file - ?
			unsigned score2 = matrix.sumCrossings();
			do {
				score = score2;
				assert(matrix.m_light);
				bubblePass(config,matrix,affectedRanks,dir,way,switchable,crossing);
				score2 = matrix.sumCrossings();
				assert(score2<=score);
			}
			while(score2<score);
			score = score2;

			if(reportEnabled(r_crossopt)) {
				char buf[10];
				sprintf(buf,"crossings pass %d",pass);
				loops.Field(r_crossopt,buf,score);
			}

			if(score<best) {
				backup = config.ranking;
				backupM = matrix;
				best = score;
				bestPass = pass;
				tired = 0;
			}
			else 
				tired++;
			pass++;
		}
		if(score>best || tired==TIRE) {
			config.Restore(backup);
			//matrix.recompute();
			matrix = backupM;
			tired = 0;
		}
	}
	if(reportEnabled(r_crossopt))
		for(;pass<passes;++pass) {
			char buf[10];
			sprintf(buf,"crossings pass %d",pass);
			loops.Field(r_crossopt,buf,0);
		}

	if(score>=best) {
		config.Restore(backup);
	}
	loops.Field(r_crossopt,"model edge crossings",best);

	// sift out node crossings
	matrix.m_light = false;
	matrix.recompute();
	score = matrix.sumCrossings();
	loops.Field(r_crossopt,"weighted crossings before heavy pass",score);
	/*
	report(r_crossopt,"%d node-node crossings; %d node-edge crossings; %d edge-edge crossings\n",score/(NODECROSS_PENALTY*NODECROSS_PENALTY),
		(score/NODECROSS_PENALTY)%NODECROSS_PENALTY,score%NODECROSS_PENALTY);
	*/
	if(score<NODECROSS_PENALTY) {
		loops.Field(r_crossopt,"weighted crossings after heavy pass",-1);
		return;
	}
	pass = 0;
	passes = 4;
	crossing.m_allowEqual = true;
	// sifting out upward or downward may be better - try both.
	bool improved;
	do {
		improved = false;
		backup = config.ranking;
		bubblePass(config,matrix,affectedRanks,DOWN,RIGHT,switchable,crossing);
		unsigned down = matrix.sumCrossings();
		Config::Ranks backup2 = config.ranking;
		config.Restore(backup);
		matrix.recompute();
		bubblePass(config,matrix,affectedRanks,UP,RIGHT,switchable,crossing);
		unsigned up = matrix.sumCrossings();
		if(down<score && down<up) {
			config.Restore(backup2);
			matrix.recompute();
			score = down;
			improved = true;
		}
		else if(up<score) {
			score = up;
			improved = true;
		}
	}
	while(improved);
	loops.Field(r_crossopt,"weighted crossings after heavy pass",score);
	// absolutely must not leave here with nodes crossing nodes!!
	assert(score<NODECROSS_PENALTY*NODECROSS_PENALTY);
}