Exemplo n.º 1
0
/*
   Solve the overconstrained linear system   Ma = b   using a least
   squares error (pseudo inverse) approach.
*/
int solve_system (dmat M, dmat a, dmat b)
/*
int       solve_system (M, a, b)
    dmat      M,
              a,
              b;
*/
  {
  dmat	Mt,MtM,Mdag;
  int		error;

  if ((M.ub1 - M.lb1) < (M.ub2 - M.lb2))
    {
    fprintf (stderr, "solve_system: matrix M has more columns than rows\n");
    return (-1);
    }
  Mt = newdmat (M.lb2, M.ub2, M.lb1, M.ub1, &error);
  if (error)
    { fprintf (stderr, "solve_system: unable to allocate matrix M_transpose\n"); return (-1); }
  if (transpose (M, Mt)!=0)
    { fprintf (stderr, "solve_system: unable to transpose matrix M\n"); return (-1); }
  MtM = newdmat (M.lb2, M.ub2, M.lb2, M.ub2, &error);
  if (error)
    { fprintf (stderr, "solve_system: unable to allocate matrix M_transpose_M\n"); return (-1); }
  if (matmul (Mt, M, MtM)!=0)
    { fprintf (stderr, "solve_system: unable to compute matrix product of M_transpose and M\n"); return (-1); }
  if (fabs (matinvert (MtM)) < 0.001)
    {
    fprintf (stderr, "solve_system: determinant of matrix M_transpose_M is too small\n");
    return (-1);
    }
  /*
  if (errno) {
   fprintf (stderr, "solve_system: error during matrix inversion\n");
   return (-1);
  }
  */

  Mdag = newdmat (M.lb2, M.ub2, M.lb1, M.ub1, &error);
  if (error)
    { fprintf (stderr, "solve_system: unable to allocate matrix M_diag\n"); return (-1); }
  if (matmul (MtM, Mt, Mdag)!=0)
    {
    fprintf (stderr, "solve_system: unable to compute matrix product of M_transpose_M and M_transpose\n");
    return (-1);
    }
  if (matmul (Mdag, b, a)!=0)
    {
    fprintf (stderr, "solve_system: unable to compute matrix product of M_diag and b\n");
    return (-1);
    }
  freemat (Mt); freemat (MtM); freemat (Mdag);
  return 0;
  }
Exemplo n.º 2
0
/*
   Solve the overconstrained linear system   Ma = b   using a least
   squares error (pseudo inverse) approach.
*/
int       solve_system (dmat M, dmat a,dmat  b)
    
{
    dmat      Mt,
              MtM,
              Mdag;
	//AfxMessageBox("S1");
    if ((M.ub1 - M.lb1) < (M.ub2 - M.lb2)) {
	fprintf (stderr, "solve_system: matrix M has more columns than rows\n");
	return (-1);
    }

	//AfxMessageBox("S2");
    Mt = newdmat (M.lb2, M.ub2, M.lb1, M.ub1, &errno);
    if (errno) {
	fprintf (stderr, "solve_system: unable to allocate matrix M_transpose\n");
	return (-2);
    }

	//AfxMessageBox("S3");
    transpose (M, Mt);
    if (errno) {
	fprintf (stderr, "solve_system: unable to transpose matrix M\n");
	return (-3);
    }

	//AfxMessageBox("S4");
    MtM = newdmat (M.lb2, M.ub2, M.lb2, M.ub2, &errno);
    if (errno) {
	fprintf (stderr, "solve_system: unable to allocate matrix M_transpose_M\n");
	return (-4);
    }

	//AfxMessageBox("S5");
    matmul (Mt, M, MtM);
    if (errno) {
	fprintf (stderr, "solve_system: unable to compute matrix product of M_transpose and M\n");
	return (-5);
    }
//modified by Dickson
	//AfxMessageBox("S6");
    
	double aa=fabs (matinvert (MtM));
	//AfxMessageBox("S7");
    //if (aa < 0.001) {
	//CString  str;
	//str.Format("determinant=%f",aa);
	//AfxMessageBox("S71");
	//AfxMessageBox(str);
	//AfxMessageBox("S8");
    
    if (aa < 0.001) {
		
	fprintf (stderr, "solve_system: determinant of matrix M_transpose_M is too small\n");
	return (-6);
    }

    if (errno) {
	fprintf (stderr, "solve_system: error during matrix inversion\n");
	return (-7);
    }

    Mdag = newdmat (M.lb2, M.ub2, M.lb1, M.ub1, &errno);
    if (errno) {
	fprintf (stderr, "solve_system: unable to allocate matrix M_diag\n");
	return (-8);
    }

    matmul (MtM, Mt, Mdag);
    if (errno) {
	fprintf (stderr, "solve_system: unable to compute matrix product of M_transpose_M and M_transpose\n");
	return (-9);
    }

    matmul (Mdag, b, a);
    if (errno) {
	fprintf (stderr, "solve_system: unable to compute matrix product of M_diag and b\n");
	return (-10);
    }

    freemat (Mt);
    freemat (MtM);
    freemat (Mdag);

    return 0;
}