<commit_before>import * as S from '../common/lazyseq';

import * as pg from './periodic';
import * as ps from './symmetries';

import { rationalLinearAlgebra as ops,
         rationalLinearAlgebraModular } from '../arithmetic/types';


class Adjustment {
  constructor(dim) {
    this.dim = dim;
    this.vectors = [];
    this.matrix = null;
  }

  solve(vecIn) {
    if (this.vectors.length)
      return ops.transposed(ops.solve(
        ops.transposed(this.vectors), ops.transposed(vecIn)
      ));
  }

  add(vecIn) {
    if (ops.sgn(vecIn) == 0)
      return vecIn;
    else if (this.matrix != null)
      return ops.times(vecIn, this.matrix);
    else {
      const vecOut = this.solve(vecIn);

      if (vecOut)
        return vecOut[0].concat(ops.vector(this.dim - vecOut[0].length));
      else {
        this.vectors.push(vecIn);
        if (this.vectors.length == this.dim)
          this.matrix = ops.inverse(this.vectors);
        return ops.unitVector(this.dim, this.vectors.length - 1);
      }
    }
  }
};


const _traversal = function*(graph, v0, transform) {
  const zero = ops.vector(graph.dim);
  const pos = pg.barycentricPlacement(graph);

  const old2new = {[v0]: 1};
  const newPos = { [v0]: ops.times(pos[v0], transform) };
  const queue = [[v0, zero]];
  const adjustment = new Adjustment(graph.dim);
  let next = 2;

  while (queue.length) {
    const [vo, vShift] = queue.shift();
    const vn = old2new[vo];

    const neighbors = [];
    for (const e of pg.incidences(graph)[vo]) {
      const w = e.tail;
      const s = ops.plus(vShift, ops.times(e.shift, transform));
      neighbors.push([w, s, ops.plus(s, ops.times(pos[w], transform))]);
    }

    neighbors.sort(([wa, sa, pa], [wb, sb, pb]) => ops.cmp(pa, pb));

    for (const [wo, s, p] of neighbors) {
      const wn = old2new[wo];<commit_msg>invariant: further refactor vector manipulation in Adjustment class<commit_after>import * as S from '../common/lazyseq';

import * as pg from './periodic';
import * as ps from './symmetries';

import { rationalLinearAlgebra as ops,
         rationalLinearAlgebraModular } from '../arithmetic/types';


class Adjustment {
  constructor(dim) {
    this.dim = dim;
    this.vectors = [];
    this.matrix = null;
  }

  add(vecIn) {
    if (ops.sgn(vecIn) == 0)
      return vecIn;
    else if (this.matrix != null)
      return ops.times(vecIn, this.matrix);
    else if (ops.rank(this.vectors.concat([vecIn])) > this.vectors.length) {
      this.vectors.push(vecIn);
      if (this.vectors.length == this.dim)
        this.matrix = ops.inverse(this.vectors);
      return ops.unitVector(this.dim, this.vectors.length - 1);
    }
    else {
      const tmp = ops.transposed(ops.solve(
        ops.transposed(this.vectors), ops.transposed(vecIn)
      ));
      return tmp[0].concat(ops.vector(this.dim - tmp[0].length));
    }
  }
};


const _traversal = function*(graph, v0, transform) {
  const zero = ops.vector(graph.dim);
  const pos = pg.barycentricPlacement(graph);

  const old2new = {[v0]: 1};
  const newPos = { [v0]: ops.times(pos[v0], transform) };
  const queue = [[v0, zero]];
  const adjustment = new Adjustment(graph.dim);
  let next = 2;

  while (queue.length) {
    const [vo, vShift] = queue.shift();
    const vn = old2new[vo];

    const neighbors = [];
    for (const e of pg.incidences(graph)[vo]) {
      const w = e.tail;
      const s = ops.plus(vShift, ops.times(e.shift, transform));
      neighbors.push([w, s, ops.plus(s, ops.times(pos[w], transform))]);
    }

    neighbors.sort(([wa, sa, pa], [wb, sb, pb]) => ops.cmp(pa, pb));

    for (const [wo, s, p] of neighbors) {
      const wn = old2new[wo];
{'fraction_non_alphanumeric': 0.16116716662121625, 'fraction_numerical': 0.00763566948459231, 'mean_word_length': 2.832810867293626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '482619', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1263, 'n_words': 298}