/* array01 - simple tests for MathArray from libMathArray

   Copyright (C) 1995, Adam Fedor

*/

#ifdef NEXT_FOUNDATION
#import <foundation/NSValue.h>
#import <foundation/NSRange.h>
#else
#include <Foundation/NSValue.h>
#include <Foundation/NSRange.h>
#endif
#include <stdio.h>
#include <math.h>
#include "MathArray/MathArray.h"
#include "MathArray/MaskedException.h"
#include "MathArray/MAValue.h"
#include "MathArray/MAValueData.h"

id <NSNumber,ComplexNumber>
array_func(id <NSNumber,ComplexNumber>number, unsigned *index, void *info)
{
    return [MANumber numberWithInt:index[0]];
}

int
main()
{
  MathArray *array;
  MathArray *subarray;
  NSRange range;
  unsigned  index[2];
  int i;

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

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

  printf("Perform user function...\n");
  [array maPerformFunction:array_func userInfo:NULL];

  printf("Extract subarray...\n");
  range.location = 3;
  range.length = 4;
  subarray = [array arraySubrange:&range];
  printf("Subarray has total %d (should be 18)\n", 
	[[subarray maTotal] intValue]);
  printf("Insert subarray... (Should be 0 1 2 3 4 3 4 5 6 9)\n");
  index[0] = 5;
  [array setArray:subarray atIndex:index];
  for (i=0; i < [[array mathData] count]; i++) 
      printf("%d ", [(NSNumber *)[[array mathData] valueAtIndex:i] intValue]);
  printf("\n");

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

  printf("Perform user function...\n");
  [array maPerformFunction:array_func userInfo:NULL];
  index[0] = 5;
  index[1] = 5;
  printf("Insert subarray...\n");
  [array setArray:subarray atIndex:index];
  printf("Array has total %d (should be 1898)\n", 
	[[array maTotal] intValue]);

  printf("Square array (by matrix multiplication)...\n");
  [array maMatrixMultiply:[MA_TEMPORARY(array) maTranspose]];
  printf("Array has total %d (should be 360246)\n", 
	[[array maTotal] intValue]);

  printf("Where array is greater than 2470...\n");
  [[array maGreater:[NSNumber numberWithInt:2470]] maWhere];
  printf("Array has size %u,  total %d (should be 26, 9324)\n", 
	[array sizes][0], [[array maTotal] intValue]);

  return 0;
}
