File: MixtureLookup.cpp

package info (click to toggle)
dyssol 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,184 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (167 lines) | stat: -rw-r--r-- 4,846 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "MixtureLookup.h"
#include "ContainerFunctions.h"
#include <utility>

CMixtureLookup::CMixtureLookup(std::vector<CDependentValues> _components)
	: m_componets{ std::move(_components) }
{
	m_weights.resize(m_componets.size(), 1.0);
	Update();
}

CMixtureLookup::CMixtureLookup(std::vector<CDependentValues> _components, std::vector<double> _weights)
	: m_componets{ std::move(_components) }
	, m_weights{ std::move(_weights) }
{
	if (m_componets.size() == m_weights.size())
		Update();
	else
		Clear();
}

void CMixtureLookup::AddComponent(const CDependentValues& _component, double _weight)
{
	m_componets.push_back(_component);
	m_weights.push_back(_weight);
	Update();
}

void CMixtureLookup::RemoveComponent(size_t _index)
{
	if (_index >= m_componets.size()) return;
	m_componets.erase(m_componets.begin() + _index);
	m_weights.erase(m_weights.begin() + _index);
	Update();
}

size_t CMixtureLookup::ComponentsNumber() const
{
	return m_componets.size();
}

size_t CMixtureLookup::Size() const
{
	return m_table.Size();
}

void CMixtureLookup::SetWeights(const std::vector<double>& _weights)
{
	if (_weights.size() != m_componets.size()) return;
	m_weights = _weights;
	Update();
}

std::vector<double> CMixtureLookup::GetWeights() const
{
	return m_weights;
}

double CMixtureLookup::GetLeft(double _right) const
{
	return m_table.GetLeft(_right);
}

double CMixtureLookup::GetRight(double _left) const
{
	return m_table.GetRight(_left);
}

void CMixtureLookup::Set(const CDependentValues& _component, double _weight)
{
	m_table.SetLeftToRight(_component);
	m_table.Mult(_weight);
}

void CMixtureLookup::Add(double _value)
{
	m_table.Add(_value);
}

void CMixtureLookup::Add(const CDependentValues& _component, double _weight)
{
	if (m_table.IsEmpty())
		Set(_component, _weight);
	else
		m_table.AddMult(_component, _weight);
}

void CMixtureLookup::Add(const CMixtureLookup& _table, double _weight)
{
	if (_table.ComponentsNumber() != ComponentsNumber()) return;
	Add(_table.m_table.GetLeftToRightTable(), _weight);
}

void CMixtureLookup::Multiply(double _value)
{
	m_table.Mult(_value);
}

bool CMixtureLookup::operator==(const CMixtureLookup& _other) const
{
	return m_table == _other.m_table && m_componets == _other.m_componets && m_weights == _other.m_weights;
}

void CMixtureLookup::Clear()
{
	m_table.Clear();
	m_componets.clear();
	m_weights.clear();
}

void CMixtureLookup::Update()
{
	std::vector<double> resParams;
	for (auto& m_componet : m_componets)
		resParams = VectorsUnionSorted(resParams, m_componet.GetParamsList());
	std::vector allValues(m_componets.size(), std::vector<double>(resParams.size()));
	for (size_t i = 0; i < m_componets.size(); ++i)
		for (size_t j = 0; j < resParams.size(); ++j)
			allValues[i][j] = m_componets[i].GetValue(resParams[j]);
	for (size_t i = 0; i < m_componets.size(); ++i)
		std::transform(allValues[i].begin(), allValues[i].end(), allValues[i].begin(), [&](auto v) { return v * m_weights[i]; });
	std::vector resValues(resParams.size(), 0.0);
	for (size_t i = 0; i < m_componets.size(); ++i)
		for (size_t j = 0; j < resParams.size(); ++j)
			resValues[j] += allValues[i][j];
	m_table.SetLeftToRight(CDependentValues{ resParams, resValues });
}

void CMixtureLookup::CTwoWayMapExt::Add(double _value)
{
	auto values = m_direct.GetValuesList();
	std::transform(values.begin(), values.end(), values.begin(), [&](double v) { return v + _value; });
	m_direct.SetValuesList(values);
	m_revert = Reverted(m_direct);
}

void CMixtureLookup::CTwoWayMapExt::Add(const CDependentValues& _table)
{
	// TODO: check performance
	const auto unique = Unique(_table);
	const auto params = VectorsUnionSorted(m_direct.GetParamsList(), unique.GetParamsList());
	const CDependentValues copy = m_direct; // can not directly use m_direct because of the interpolation
	for (const auto& p : params)
		m_direct.SetValue(p, copy.GetValue(p) + unique.GetValue(p));
	m_revert = Reverted(m_direct);
}

void CMixtureLookup::CTwoWayMapExt::AddMult(const CDependentValues& _table, double _value)
{
	// TODO: check performance
	const auto unique = Unique(_table);
	const auto params = VectorsUnionSorted(m_direct.GetParamsList(), unique.GetParamsList());
	const CDependentValues copy = m_direct; // can not directly use m_direct because of the interpolation
	for (const auto& p : params)
		m_direct.SetValue(p, copy.GetValue(p) + unique.GetValue(p) * _value);
	m_revert = Reverted(m_direct);
}

void CMixtureLookup::CTwoWayMapExt::Mult(double _value)
{
	auto values = m_direct.GetValuesList();
	std::transform(values.begin(), values.end(), values.begin(), [&](double v) { return v * _value; });
	m_direct.SetValuesList(values);
	m_revert = Reverted(m_direct);
}