Пример #1
0
int main (int argc, char **argv)
{
   unsigned int n;
   unsigned int i, j;
   double **a, *b, **a1, *b1;

   n = 50;

   a = JDMdouble_matrix (n, n);
   a1 = JDMdouble_matrix (n, n);
   b = JDMdouble_vector (n);
   b1 = JDMdouble_vector (n);

   for (i = 0; i < n; i++)
     {
	for (j = 0; j < n; j++)
	  a[i][j] = a1[i][j] = JDMrandom ();

	b[i] = b1[i] = JDMrandom ();
     }

   if (0 == JDMgauss_jordan (a, b, n))
     {
	for (i = 0; i < n; i++)
	  {
	     double sum = 0;
	     for (j = 0; j < n; j++)
	       sum += a[i][j] * b1[j];

	     fprintf (stdout, "%e\n", sum - b[i]);
	  }
     }

   return 0;
}
Пример #2
0
static int test_inverse (unsigned int n, double tol,
                         int (*fun) (double **, double **, unsigned int))
{
    Special_Matrix_Type *s;
    double **a, **b;

    if (NULL == (a = JDMdouble_matrix (n, n)))
        return -1;

    if (NULL == (b = JDMdouble_matrix (n, n)))
    {
        JDMfree_double_matrix (a, n);
        return -1;
    }

    s = Special_Matrices;
    while (s->f != NULL)
    {
        int status;
        double val;

        init_matrix (a, n, s->f);
        status = test_this_inverse (a, b, n, fun, &val);
        if (status == -1)
            return -1;

        if (status == s->is_singular)
        {
            fprintf (stderr, "%s: failed singularity test\n", s->name);
            s++;
            continue;
        }

        if (s->is_singular)
        {
            s++;
            continue;
        }

        if (val > tol)
            fprintf (stderr, "%s failed: tolerance exceeded (%e)\n", s->name, val);

        s++;
    }

    return 0;
}
Пример #3
0
Файл: math.c Проект: hankem/ISIS
static int pop_linear_system (Linear_System_Type *t) /*{{{*/
{
   SLang_Array_Type *sl_a=NULL, *sl_b=NULL;
   double **a=NULL, *b=NULL;
   SLindex_Type i, j, n, dims[2];
   int status = -1;

   t->a = NULL;
   t->b = NULL;
   t->n = 0;

   if ((-1 == SLang_pop_array_of_type (&sl_b, SLANG_DOUBLE_TYPE))
       || (-1 == SLang_pop_array_of_type (&sl_a, SLANG_DOUBLE_TYPE)))
     goto return_error;

   n = sl_b->num_elements;

   if (sl_a->num_elements != (unsigned int) n*n)
     goto return_error;

   if ((NULL == (a = JDMdouble_matrix (n, n)))
       || (NULL == (b = JDMdouble_vector (n))))
     goto return_error;

   memcpy ((char *)b, (char *)sl_b->data, n * sizeof(double));
   for (i = 0; i < n; i++)
     {
        double *ai = a[i];
        if (-1 == SLang_get_array_element (sl_b, &i, &b[i]))
          goto return_error;
        dims[0] = i;
        for (j = 0; j < n; j++)
          {
             double aij;
             dims[1] = j;
             if (-1 == SLang_get_array_element (sl_a, dims, &aij))
               goto return_error;
             ai[j] = aij;
          }
     }

   t->a = a;
   t->b = b;
   t->n = n;

   status = 0;
return_error:
   SLang_free_array (sl_a);
   SLang_free_array (sl_b);
   return status;
}