fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double dot_product (double *a, double *b, size_t n) {
  5. double acc=0;
  6. for (size_t i=0; i < n; ++i)
  7. acc += a[i]*b[i];
  8. return acc;
  9. }
  10.  
  11. int is_ortho (double *m, size_t n) {
  12. for (size_t i=0; i < n; ++i) {
  13. for (size_t j=i; j < n; ++j) {
  14. double p=dot_product (m+i*n, m+j*n, n);
  15. if (i==j) { if (fabs(p-1) > 1e-6) return 0; }
  16. else { if ( p > 1e-6) return 0; }
  17. }
  18. }
  19. return 1;
  20. }
  21.  
  22. int main(void) {
  23. double m1[] = { 0.96, -0.28, 0.28, 0.96 };
  24. double m2[] = { 1, 0, 0, -1 };
  25. double m3[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  26. double m4[] = { 1 };
  27. printf ("%d", is_ortho(m1, 2));
  28. printf ("%d", is_ortho(m2, 2));
  29. printf ("%d", is_ortho(m3, 3));
  30. printf ("%d", is_ortho(m4, 1));
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
1101