/* array01 - simple tests for MathArray from libMathArray

   Copyright (C) 1995, Adam Fedor

*/

#ifdef NEXT_FOUNDATION
#include <foundation/NSValue.h>
#define  reason exceptionReason
#else
#include <Foundation/NSValue.h>
#endif
#include <stdio.h>
#include <math.h>
#include "MathArray/MathArray.h"
#include "MathArray/MaskedException.h"
#include "MathArray/MAValue.h"

/* Replace value by the exponent of it's index */
id <NSNumber,ComplexNumber>
exp_array(id <NSNumber,ComplexNumber>number, unsigned *index, void *info)
{
  return [MANumber numberWithDouble:index[0]*index[1]];
}

int
main()
{
  MathArray *array;
  MANumber  *number;
  complex_double cd;
  unsigned  index[2];

  [MaskedException bodysnatchNSException];
  [MaskedException setMaskForAllExceptions:AbortException];

  printf("Create 100 point vector...\n");
  array = [MathArray maWithVector:100 objCType:@encode(int)];

  [array maAdd:[NSNumber numberWithFloat:3.01]];
  printf("Add 3.01, Total is %f (should be 301.0)\n", 
	[[array maTotal] floatValue]);

  [array maDivide:[NSNumber numberWithInt:2]];
  printf("Divide 2, Total is %f (should be 150.5)\n", 
	[[array maTotal] floatValue]);

  [array maCos];
  printf("Take cos, Total is %f (should be 6.57488)\n", 
	[[array maTotal] floatValue]);

  number = [MANumber numberWithComplexDouble:((complex_double){2, 1})];
  [array maMultiply:number];
  cd = [[array maMinimumValue] complexDoubleValue];
  printf("Mult 2+i, Minimum is {%f, %f} (should be {0.131498, 0.0657489})\n", 
	cd.real, cd.imag);

  /* Matrices */
  printf("Create 10X10 matrix...\n");
  array = [MathArray maMatrixWithCols:10 rows:10 objCType:NULL];

  printf("Perform user function...\n");
  [array maPerformFunction:exp_array userInfo:NULL];
  index[0] = 3;
  index[1] = 3;
  printf("Value at (3,3) is %g (should be 9)\n", 
	[[array arrayValueAtIndex:index] doubleValue]);
  printf("Total is %g (should be 2025)\n", [[array maTotal] doubleValue]);

  /* Try an illegal operation */
  printf("Intentionally divide by 0 (should get error)\n");
  NS_DURING
    [array maDivide: [NSNumber numberWithFloat:0.0]];
  NS_HANDLER
    printf("  Caught Exception - %s\n", [[exception reason] cString]);
  NS_ENDHANDLER
  
  return 0;
}
