<commit_before>'use strict';

var I = require('immutable');


var vector = function vector(scalar, zero) {

  var Vector = I.Record({
    size: undefined,
    data: undefined
  });

  Vector.prototype.toString = function() {
    return '<vector: '+this.data+'>'
  };

  var get = function get(v, i) {
    return v.data.get(i);
  };

  var make = function make(data) {
    var tmp = I.List(data);
    if (tmp.size == 0)
      throw new Error('must have positive size');

    return new Vector({
      size: tmp.size,
      data: tmp
    });
  };

  var constant = function constant(size, value) {
    var x = value === undefined ? zero : value;
    return make(I.List(I.Repeat(x, size)));
  };

  var set = function set(v, i, x) {
    return make(v.data.set(i, x));
  };

  var update = function update(v, i, fn) {
    return make(v.data.update(i, fn));
  };

  var plus = function plus(v, w) {
    if (v.size != w.size)
      throw new Error('shapes do not match');

    return make(I.Range(0, v.size).map(function(i) {
      return scalar.plus(get(v, i), get(w, i));
    }));
  };

  var minus = function minus(v, w) {
    if (v.size != w.size)
      throw new Error('shapes do not match');

    return make(I.Range(0, v.size).map(function(i) {
      return scalar.minus(get(v, i), get(w, i));
    }));
  };

  var scaled = function scaled(f, v) {
    return make(v.data.map(function(x) { return scalar.times(f, x); }));
  };

  var cmp = function cmp(v, w) {
    var d = I.Range(0, Math.min(v.size, w.size))
      .map(function(i) { return scalar.cmp(get(v, i), get(w, i)); })
      .filter(function(x) { return !!x; })
      .first();

    return d || v.size - w.size;
  };

  var dotProduct = function dotProduct(v, w) {
    if (v.size != w.size)
      throw new Error('shapes do not match');

    return I.Range(0, v.size)
      .map(function(i) { return scalar.times(get(v, i), get(w, i)); })
      .reduce(scalar.plus, zero);
  };

  var norm = function norm(v) {
    return Math.sqrt(scalar.toJS(dotProduct(v, v)));
  };

  var normalized = function normalized(v) {
    return scaled(1 / norm(v), v);
  };

  return {
    get       : get,
    make      : make,
    constant  : constant,
    set       : set,
    update    : update,
    plus      : plus,
    minus     : minus,
    scaled    : scaled,
    cmp       : cmp,
    dotProduct: dotProduct,
    norm      : norm,
    normalized: normalized
  };
};


module.exports = vector;<commit_msg>add a cross product function<commit_after>'use strict';

var I = require('immutable');


var vector = function vector(scalar, zero) {

  var Vector = I.Record({
    size: undefined,
    data: undefined
  });

  Vector.prototype.toString = function() {
    return '<vector: '+this.data+'>'
  };

  var get = function get(v, i) {
    return v.data.get(i);
  };

  var make = function make(data) {
    var tmp = I.List(data);
    if (tmp.size == 0)
      throw new Error('must have positive size');

    return new Vector({
      size: tmp.size,
      data: tmp
    });
  };

  var constant = function constant(size, value) {
    var x = value === undefined ? zero : value;
    return make(I.List(I.Repeat(x, size)));
  };

  var set = function set(v, i, x) {
    return make(v.data.set(i, x));
  };

  var update = function update(v, i, fn) {
    return make(v.data.update(i, fn));
  };

  var plus = function plus(v, w) {
    if (v.size != w.size)
      throw new Error('shapes do not match');

    return make(I.Range(0, v.size).map(function(i) {
      return scalar.plus(get(v, i), get(w, i));
    }));
  };

  var minus = function minus(v, w) {
    if (v.size != w.size)
      throw new Error('shapes do not match');

    return make(I.Range(0, v.size).map(function(i) {
      return scalar.minus(get(v, i), get(w, i));
    }));
  };

  var scaled = function scaled(f, v) {
    return make(v.data.map(function(x) { return scalar.times(f, x); }));
  };

  var cmp = function cmp(v, w) {
    var d = I.Range(0, Math.min(v.size, w.size))
      .map(function(i) { return scalar.cmp(get(v, i), get(w, i)); })
      .filter(function(x) { return !!x; })
      .first();

    return d || v.size - w.size;
  };

  var dotProduct = function dotProduct(v, w) {
    if (v.size != w.size)
      throw new Error('shapes do not match');

    return I.Range(0, v.size)
      .map(function(i) { return scalar.times(get(v, i), get(w, i)); })
      .reduce(scalar.plus, zero);
  };

  var crossProduct = function crossProduct(v, w) {
    if (v.size != 3 || w.size != 3)
      throw new Error('both vector must be three-dimensional');

    return make([
      scalar.minus(scalar.times(get(v, 1), get(w, 2)),
                   scalar.times(get(v, 2), get(w, 1))),
      scalar.minus(scalar.times(get(v, 2), get(w, 0)),
                   scalar.times(get(v, 0), get(w, 2))),
      scalar.minus(scalar.times(get(v, 0), get(w, 1)),
                   scalar.times(get(v, 1), get(w, 0)))
    ]);
  };

  var norm = function norm(v) {
    return Math.sqrt(scalar.toJS(dotProduct(v, v)));
  };

  var normalized = function normalized(v) {
    return scaled(1 / norm(v), v);
  };

  return {
    get       : get,
    make      : make,
    constant  : constant,
    set       : set,
    update    : update,
    plus      : plus,
    minus     : minus,
    scaled    : scaled,
    cmp       : cmp,
    dotProduct: dotProduct,
    crossProduct: crossProduct,
    norm      : norm,
    normalized: normalized
  };
};


module.exports = vector;
{'fraction_non_alphanumeric': 0.1715121594441397, 'fraction_numerical': 0.004754068385445237, 'mean_word_length': 2.393300248138958, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7024884', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1919, 'n_words': 506}