Søren Hauberg wrote:
> Sorry if I came off a bit harsh in my previous mail.
>
> I had a quick look at your code; I can tell you what the problem is, but
> I cannot tell you how to solve as I don't know what your code is
> supposed to do and I don't know the SparseMatrix class very well
> (perhaps other people on the list can help).
>
> Basically 'a(i, b.xridx(k))' (the first argument to the 'calc_tnorm'
> call) gives you problems as this actually creates an element in the
> matrix, which is not what you want. Now you could avoid this by
> declaring 'a' and 'b' as being constant. This does, however, not work in
> your case as you are calling the 'xridx' function which is non-const.
>
> Not sure what the solution is as that depends on what you actually want
> to accomplish.
>
>   
No time to really look at this, but does the attached help? The major
changes are

- Declare the matrices a and b as const to ensure that a.data, etc don't
add elements to these matrices..
- Limit the number of elements to create in the sparse matrix c to
a.nnz()+b.nnz() as this seems to be the maximum it can have to me,
though it can easily be less
- Operate on the transpose of the matrix C as the compressed row storage
of the Octave sparse matrix makes this more logical. Do a final
transpose to get the data in the right sense


You might be able to get things faster if  you eliminate the
a(i,b.xridx(k)) call. This might be done by either working with the
transpose of a, though at the cost of more memory.

-- 
David Bateman                                dbate...@dbateman.org
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)

#include <octave/oct.h>
#define NUM_ARG 2
#define HELP " Help della funzione"

double calc_tnorm(double first, double second);

DEFUN_DLD (spmaxmin_ST_new, args, nargout, HELP)
{
  if (args.length() != NUM_ARG)	
    print_usage();
  else
    {
      const SparseMatrix a = args(0).sparse_matrix_value();
      const SparseMatrix b = args(1).sparse_matrix_value();
      dim_vector dimsA = a.dims();
      octave_idx_type rowsA = dimsA(0);
      octave_idx_type colsA = dimsA(1);
      octave_idx_type nnzA = a.nnz();

      dim_vector dimsB = b.dims();
      octave_idx_type rowsB = dimsB(0);
      octave_idx_type colsB = dimsB(1);
      octave_idx_type nnzB = b.nnz();
	
      if (colsA != rowsB)
	print_usage();
      else
	{
	  octave_idx_type i,j,k;
	  double mx,mn;
	  octave_idx_type kk = 0;

	  //il terzo parametro è il numero di elementi nonzero della matrice
	  //visto che non posso saperlo al momento della creazione, lo metto 
	  //al numero massimo poi richiamerò una funzione che comprimerà la
	  //matrice al numero utile di elementi nonzero
	  SparseMatrix c = SparseMatrix(colsB, rowsA, nnzA + nnzB);
	  octave_idx_type rowsC = colsB;
	  octave_idx_type colsC = rowsA;

	  c.xcidx(0) = 0;

	  for (i = 0; i < rowsA; i++)
	    {
	      for(j = 0; j < colsB; j++)
		{
		  mx = 0;
					
		  // ciclo solo sugli elementi nonzero della jesima colonna di b
		  for (k = b.cidx(j); k <= (b.cidx(j+1) - 1); k++)
		    {	
		      // b.data(k) mi da il valore del kesimo elemento
		      // del sottovettore degli elementi nonzero della
		      // jesima colonna di b
		      // b.ridx(k) mi da la riga del kesimo elemento
		      // del sottovettore degli elementi nonzero della
		      // jesima colonna di b
		      mn = calc_tnorm(a(i, b.ridx(k)), b.data(k));
		      if (mn > mx)
			mx = mn;
		    }
		
		  //aggiungo il controllo che l'elemento aggiunto alla matrice
		  // sparsa non sia uno 0 perchè altrimenti bisognerebbe 
		  // comprimere la matrice sparsa con la rimozione degli 0 
		  //(molto costosa)
		  if (mx != 0)
		    {
		      c.xridx(kk) = j;
		      c.xdata(kk++) = mx;
		    }
		}
	      c.xcidx(i+1) = kk;
	    }

	  //funzione che comrpime la matrice sparsa riducendo il numero di
	  //elementi nonzero. Se le si passa true come parametro si vuole che 
	  // la funzione rimuova eventuali 0 dalla matrice sparsa poiche 
	  // questo è molto dispendioso computazionalmente semplicemente è
	  //meglio non aggiugnere gli elementi uguali a 0 alla matrice sparsa
	  c.maybe_compress();
	  return octave_value(c.transpose());
	}
    }
}


double calc_tnorm(double first, double second)
{
  if(first < second)
    return first;
  else
    return second;
}
------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to