'use strict';

var cwise = require('cwise');

exports.swap = cwise({
  args: ['array', 'array'],
  body: function(a,b) {
    var tmp = a;
    a = b;
    b = tmp;
  }
});

exports.scal = cwise({
  args: ['scalar', 'array'],
  body: function(alpha,x) {
    x *= alpha;
  }
});

exports.copy = cwise({
  args: ['array', 'array'],
  body: function(x,y) {
    y = x;
  }
});

exports.axpy = cwise({
  args:['scalar', 'array', 'array'],
  body: function(alpha, x, y) {
    y += alpha * x;
  }
});

exports.dot = cwise({
  args:['array', 'array'],
  pre: function() {
    this.sum = 0;
  },
  body: function(a,b) {
    this.sum += a * b;
  },
  post: function() {
    return this.sum;
  }
});

exports.cpsc = cwise({
  args:['scalar', 'array', 'array'],
  body: function(alpha, x, y) {
    y = alpha * x;
  }
});

exports.nrm2 = cwise({
  args:['array'],
  pre: function() {
    this.sum = 0;
  },
  body: function(a) {
    this.sum += a * a;
  },
  post: function() {
    return Math.sqrt(this.sum);
  }
});

exports.asum = cwise({
  args:['array'],
  pre: function() {
    this.sum = 0;
  },
  body: function(a) {
    this.sum += Math.abs(a);
  },
  post: function() {
    return this.sum;
  }
});

exports.iamax = cwise({
  args:['array'],
  pre: function() {
    this.maxValue = Number.NEGATIVE_INFINITY;
    this.maxIndex = 0;
    this.index = 0;
  },
  body: function(a) {
    if (a > this.maxValue) {
        this.maxValue = a;
        this.maxIndex = this.index;
    }
    this.index++;
  },
  post: function() {
    return this.maxIndex;
  }
});

@@ -1273,32 +1273,9 @@
 e = 
-Number.NEGATIVE_INFINITY
+0
 ;%0A  
@@ -1350,17 +1350,27 @@
     if (
-a
+Math.abs(a)
  %3E this.

index of first max absolute value