#include "util/HVectorBase.h"
#include <cassert>
#include <cmath>
#include "lp_data/HConst.h"
#include "stdio.h"
#include "util/HighsCDouble.h"
template <typename Real>
void HVectorBase<Real>::setup(HighsInt size_) {
size = size_;
count = 0;
index.resize(size);
array.assign(size, Real{0});
cwork.assign(size + 6400, 0); iwork.assign(size * 4, 0);
packCount = 0;
packIndex.resize(size);
packValue.resize(size);
packFlag = false;
synthetic_tick = 0;
next = 0;
}
template <typename Real>
void HVectorBase<Real>::clear() {
HighsInt dense_clear = count < 0 || count > size * 0.3;
if (dense_clear) {
array.assign(size, Real{0});
} else {
for (HighsInt i = 0; i < count; i++) {
array[index[i]] = 0;
}
}
this->clearScalars();
}
template <typename Real>
void HVectorBase<Real>::clearScalars() {
this->packFlag = false;
this->count = 0;
this->synthetic_tick = 0;
this->next = 0;
}
template <typename Real>
void HVectorBase<Real>::tight() {
HighsInt totalCount = 0;
using std::abs;
if (count < 0) {
for (auto& val : array)
if (abs(val) < kHighsTiny) val = 0;
} else {
for (HighsInt i = 0; i < count; i++) {
const HighsInt my_index = index[i];
const Real& value = array[my_index];
if (abs(value) >= kHighsTiny) {
index[totalCount++] = my_index;
} else {
array[my_index] = Real{0};
}
}
count = totalCount;
}
}
template <typename Real>
void HVectorBase<Real>::pack() {
if (!packFlag) return;
packFlag = false;
packCount = 0;
for (HighsInt i = 0; i < count; i++) {
const HighsInt ipack = index[i];
packIndex[packCount] = ipack;
packValue[packCount] = array[ipack];
packCount++;
}
}
template <typename Real>
void HVectorBase<Real>::reIndex() {
if (count >= 0 && count <= size * 0.1) return;
count = 0;
for (HighsInt i = 0; i < size; i++)
if ((double)array[i]) index[count++] = i;
}
template <typename Real>
template <typename FromReal>
void HVectorBase<Real>::copy(const HVectorBase<FromReal>* from) {
clear();
synthetic_tick = from->synthetic_tick;
const HighsInt fromCount = count = from->count;
const HighsInt* fromIndex = &from->index[0];
const FromReal* fromArray = &from->array[0];
for (HighsInt i = 0; i < fromCount; i++) {
const HighsInt iFrom = fromIndex[i];
const FromReal xFrom = fromArray[iFrom];
index[i] = iFrom;
array[iFrom] = Real(xFrom);
}
}
template <typename Real>
Real HVectorBase<Real>::norm2() const {
const HighsInt workCount = count;
const HighsInt* workIndex = &index[0];
const Real* workArray = &array[0];
Real result = Real{0};
for (HighsInt i = 0; i < workCount; i++) {
Real value = workArray[workIndex[i]];
result += value * value;
}
return result;
}
template <typename Real>
template <typename RealPivX, typename RealPiv>
void HVectorBase<Real>::saxpy(const RealPivX pivotX,
const HVectorBase<RealPiv>* pivot) {
HighsInt workCount = count;
HighsInt* workIndex = &index[0];
Real* workArray = &array[0];
const HighsInt pivotCount = pivot->count;
const HighsInt* pivotIndex = &pivot->index[0];
const RealPiv* pivotArray = &pivot->array[0];
using std::abs;
for (HighsInt k = 0; k < pivotCount; k++) {
const HighsInt iRow = pivotIndex[k];
const Real x0 = workArray[iRow];
const Real x1 = Real(x0 + pivotX * pivotArray[iRow]);
if (x0 == Real{0}) workIndex[workCount++] = iRow;
workArray[iRow] = (abs(x1) < kHighsTiny) ? kHighsZero : x1;
}
count = workCount;
}
template <typename Real>
bool HVectorBase<Real>::isEqual(const HVectorBase<Real>& v0) {
if (this->size != v0.size) return false;
if (this->count != v0.count) return false;
if (this->index != v0.index) return false;
if (this->array != v0.array) return false;
if (this->synthetic_tick != v0.synthetic_tick) return false;
return true;
}
template class HVectorBase<double>;
template void HVectorBase<double>::copy(const HVectorBase<double>*);
template void HVectorBase<double>::copy(const HVectorBase<HighsCDouble>*);
template void HVectorBase<double>::saxpy(const double,
const HVectorBase<double>*);
template void HVectorBase<double>::saxpy(const double,
const HVectorBase<HighsCDouble>*);
template void HVectorBase<double>::saxpy(const HighsCDouble,
const HVectorBase<double>*);
template void HVectorBase<double>::saxpy(const HighsCDouble,
const HVectorBase<HighsCDouble>*);
template class HVectorBase<HighsCDouble>;
template void HVectorBase<HighsCDouble>::copy(const HVectorBase<double>*);
template void HVectorBase<HighsCDouble>::copy(const HVectorBase<HighsCDouble>*);
template void HVectorBase<HighsCDouble>::saxpy(const double,
const HVectorBase<double>*);
template void HVectorBase<HighsCDouble>::saxpy(
const double, const HVectorBase<HighsCDouble>*);
template void HVectorBase<HighsCDouble>::saxpy(const HighsCDouble,
const HVectorBase<double>*);
template void HVectorBase<HighsCDouble>::saxpy(
const HighsCDouble, const HVectorBase<HighsCDouble>*);
#if 0#endif