funcs.cpp 24.9 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
/**
 * Built-in functions
 * @author Tobias Weber <tweber@ill.fr>
 * @date 20-Jun-2018
 * @license see 'LICENSE' file
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * ----------------------------------------------------------------------------
 * mag-core (part of the Takin software suite)
 * Copyright (C) 2018-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
24
25
26
 */

#include "funcs.h"
Tobias WEBER's avatar
Tobias WEBER committed
27
#include "../globals.h"
Tobias WEBER's avatar
Tobias WEBER committed
28
29
#include "tlibs2/libs/phys.h"
#include "tlibs2/libs/fit.h"
Tobias WEBER's avatar
Tobias WEBER committed
30
31
32
33
34


using t_real = t_real_cli;


Tobias WEBER's avatar
Tobias WEBER committed
35
// ----------------------------------------------------------------------------
36
37
// real functions
// ----------------------------------------------------------------------------
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

/**
 * point-wise evaluation of a real function for an array type
 */
std::shared_ptr<Symbol> call_realfunc_1arg_pointwise(const std::string& ident, std::shared_ptr<Symbol> sym)
{
	auto iter = g_funcs_real_1arg.find(ident);
	if(iter == g_funcs_real_1arg.end())
		return nullptr;

	if(sym->GetType() == SymbolType::ARRAY)
	{
		const auto& arr = dynamic_cast<const SymbolList&>(*sym).GetValue();
		std::vector<std::shared_ptr<Symbol>> arrNew;
		arrNew.reserve(arr.size());

		for(const auto& val : arr)
		{
			auto funcval = call_realfunc_1arg_pointwise(ident, val);
			arrNew.emplace_back(funcval);
		}

		return std::make_shared<SymbolList>(arrNew, false);
	}
	else if(sym->GetType() == SymbolType::REAL)
	{
		auto funcval = (*std::get<0>(iter->second))(dynamic_cast<SymbolReal&>(*sym).GetValue());
		return std::make_shared<SymbolReal>(funcval);
	}

	return nullptr;
}
Tobias WEBER's avatar
Tobias WEBER committed
70
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
71
72
73



Tobias WEBER's avatar
Tobias WEBER committed
74
75


Tobias WEBER's avatar
Tobias WEBER committed
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
// ----------------------------------------------------------------------------
// array functions
// ----------------------------------------------------------------------------

/**
 * dot product
 */
std::shared_ptr<Symbol> func_dot(std::shared_ptr<SymbolList> sym1, std::shared_ptr<SymbolList> sym2)
{
	std::shared_ptr<Symbol> symDot = std::make_shared<SymbolReal>(0);
	const auto& arr1 = sym1->GetValue();
	const auto& arr2 = sym2->GetValue();
	for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
		symDot = Symbol::add(symDot, Symbol::mul(arr1[idx], arr2[idx]));
	return symDot;
}


/**
 * cross product
 */
std::shared_ptr<Symbol> func_cross(std::shared_ptr<SymbolList> sym1, std::shared_ptr<SymbolList> sym2)
{
	SymbolReal symCross(0);
	const auto& arr1 = sym1->GetValue();
	const auto& arr2 = sym2->GetValue();

	if(arr1.size() != 3 && arr2.size() != 3)
		return nullptr;

	// components
	std::vector<std::shared_ptr<Symbol>> vec;
	vec.emplace_back(Symbol::sub(Symbol::mul(arr1[1], arr2[2]), Symbol::mul(arr1[2], arr2[1])));
	vec.emplace_back(Symbol::sub(Symbol::mul(arr1[2], arr2[0]), Symbol::mul(arr1[0], arr2[2])));
	vec.emplace_back(Symbol::sub(Symbol::mul(arr1[0], arr2[1]), Symbol::mul(arr1[1], arr2[0])));

	return std::make_shared<SymbolList>(vec, false);
}

Tobias WEBER's avatar
Tobias WEBER committed
117

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * norm
 */
std::shared_ptr<Symbol> func_norm(std::shared_ptr<SymbolList> sym)
{
	// dot
	auto symDot = func_dot(sym, sym);
	// sqrt
	return Symbol::pow(*symDot, SymbolReal(0.5));
}
// ----------------------------------------------------------------------------






// ----------------------------------------------------------------------------
// general functions
Tobias WEBER's avatar
Tobias WEBER committed
137
// ----------------------------------------------------------------------------
138

Tobias WEBER's avatar
Tobias WEBER committed
139
140
141
/**
 * typeof function
 */
Tobias WEBER's avatar
Tobias WEBER committed
142
static std::shared_ptr<Symbol> func_typeof(CliParserContext&, std::shared_ptr<Symbol> sym)
Tobias WEBER's avatar
Tobias WEBER committed
143
144
145
146
147
{
	const std::string& ty = Symbol::get_type_name(*sym);
	return std::make_shared<SymbolString>(ty);
}

Tobias WEBER's avatar
Tobias WEBER committed
148

149
150
151
/**
 * sizeof function
 */
Tobias WEBER's avatar
Tobias WEBER committed
152
static std::shared_ptr<Symbol> func_sizeof(CliParserContext&, std::shared_ptr<Symbol> sym)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
	if(sym->GetType() == SymbolType::ARRAY)
	{	// array length
		std::size_t len = dynamic_cast<SymbolList&>(*sym).GetValue().size();
		return std::make_shared<SymbolReal>(len);
	}
	else if(sym->GetType() == SymbolType::STRING)
	{	// string length
		std::size_t len = dynamic_cast<SymbolString&>(*sym).GetValue().length();
		return std::make_shared<SymbolReal>(len);
	}
	else if(sym->GetType() == SymbolType::DATASET)
	{	// number of channels in dataset
		std::size_t len = dynamic_cast<SymbolDataset&>(*sym).GetValue().GetNumChannels();
		return std::make_shared<SymbolReal>(len);
	}

	// 1 for other types
	return std::make_shared<SymbolReal>(1);
}


Tobias WEBER's avatar
Tobias WEBER committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * convert a dataset to an array
 */
std::shared_ptr<Symbol> func_array(CliParserContext & ctx, std::shared_ptr<Symbol> _sym)
{
	if(_sym->GetType() == SymbolType::DATASET)
	{
		std::vector<std::shared_ptr<Symbol>> arr;
		const auto& dataset = dynamic_cast<SymbolDataset&>(*_sym).GetValue();

		// iterate channels
		for(std::size_t ch=0; ch<dataset.GetNumChannels(); ++ch)
		{
			const auto& dat = dataset.GetChannel(ch);
			if(dat.GetNumAxes()==0 || dat.GetNumCounters()==0)
			{
				ctx.PrintError("Invalid x axis or counter data in channel ", ch, ".");
				return nullptr;
			}

			const auto& xvals = dat.GetAxis(0);
			const auto& yvals = dat.GetCounter(0);
			const auto& yerrs = dat.GetCounterErrors(0);

			std::vector<std::shared_ptr<Symbol>> x, y, yerr;

			for(std::size_t i=0; i<xvals.size(); ++i)
			{
				x.emplace_back(std::make_shared<SymbolReal>(xvals[i]));
				y.emplace_back(std::make_shared<SymbolReal>(yvals[i]));
				yerr.emplace_back(std::make_shared<SymbolReal>(yerrs[i]));
			}

			// add x, y, yerr to channel
			std::vector<std::shared_ptr<Symbol>> arrCh;
			arrCh.emplace_back(std::make_shared<SymbolList>(x, false));
			arrCh.emplace_back(std::make_shared<SymbolList>(y, false));
			arrCh.emplace_back(std::make_shared<SymbolList>(yerr, false));

			// add channel to array
			arr.emplace_back(std::make_shared<SymbolList>(arrCh, false));
		}

		return std::make_shared<SymbolList>(arr, false);
	}

	return nullptr;
}



Tobias WEBER's avatar
Tobias WEBER committed
226
227
228
/**
 * help
 */
Tobias WEBER's avatar
Tobias WEBER committed
229
std::shared_ptr<Symbol> func_help(CliParserContext&)
Tobias WEBER's avatar
Tobias WEBER committed
230
231
232
{
	std::ostringstream ostr;

Tobias WEBER's avatar
Tobias WEBER committed
233
	ostr << "<hr>Takin/Scan Browser version " << PROGRAM_VERSION << ".<br>\n";
Tobias WEBER's avatar
Tobias WEBER committed
234
	ostr << "Written by Tobias Weber &lt;tweber@ill.fr&gt;, 2018-2021.<hr><br>\n";
Tobias WEBER's avatar
Tobias WEBER committed
235

236
237
238
239
240
241
242
243
244
245
	ostr << "Type funcs() or vars() to list available functions or variables.<br>\n";

	return std::make_shared<SymbolString>(ostr.str());
}



/**
 * list of functions
 */
Tobias WEBER's avatar
Tobias WEBER committed
246
std::shared_ptr<Symbol> func_funcs(CliParserContext&)
247
248
249
{
	std::ostringstream ostr;

Tobias WEBER's avatar
Tobias WEBER committed
250

251
	if(g_funcs_real_1arg.size())
Tobias WEBER's avatar
Tobias WEBER committed
252
	{
253
254
255
		ostr << "<table border=\"1\" width=\"75%\">\n";
		ostr << "<caption><b>Real functions with one argument</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
256

257
258
259
260
261
262
263
264
265
		for(const auto& pair : g_funcs_real_1arg)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}
Tobias WEBER's avatar
Tobias WEBER committed
266
267


268
	if(g_funcs_real_2args.size())
Tobias WEBER's avatar
Tobias WEBER committed
269
	{
270
271
272
		ostr << "<table border=\"1\" width=\"75%\">\n";
		ostr << "<caption><b>Real functions with two arguments</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
273

274
275
276
277
278
279
280
281
282
		for(const auto& pair : g_funcs_real_2args)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}
Tobias WEBER's avatar
Tobias WEBER committed
283
284


285
	if(g_funcs_arr_1arg.size())
Tobias WEBER's avatar
Tobias WEBER committed
286
	{
287
288
289
		ostr << "<table border=\"1\" width=\"75%\">\n";
		ostr << "<caption><b>Array functions with one argument</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
290

291
292
293
294
295
296
297
298
299
		for(const auto& pair : g_funcs_arr_1arg)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}
Tobias WEBER's avatar
Tobias WEBER committed
300
301


302
	if(g_funcs_arr_2args.size())
Tobias WEBER's avatar
Tobias WEBER committed
303
	{
304
305
306
		ostr << "<table border=\"1\" width=\"75%\">\n";
		ostr << "<caption><b>Array functions with two arguments</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
307

308
309
310
311
312
313
314
315
316
		for(const auto& pair : g_funcs_arr_2args)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}
Tobias WEBER's avatar
Tobias WEBER committed
317
318


319
	if(g_funcs_gen_0args.size())
Tobias WEBER's avatar
Tobias WEBER committed
320
	{
321
322
323
		ostr << "<table border=\"1\" width=\"75%\">\n";
		ostr << "<caption><b>General functions with no arguments</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
324

325
326
327
328
329
330
331
332
333
		for(const auto& pair : g_funcs_gen_0args)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}
Tobias WEBER's avatar
Tobias WEBER committed
334
335


336
	if(g_funcs_gen_1arg.size())
Tobias WEBER's avatar
Tobias WEBER committed
337
	{
338
339
340
		ostr << "<table border=\"1\" width=\"75%\">\n";
		ostr << "<caption><b>General functions with one argument</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
341

342
343
344
345
346
347
348
349
350
		for(const auto& pair : g_funcs_gen_1arg)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}
Tobias WEBER's avatar
Tobias WEBER committed
351
352


353
	if(g_funcs_gen_2args.size())
Tobias WEBER's avatar
Tobias WEBER committed
354
	{
355
356
357
358
359
360
361
362
363
364
365
366
		ostr << "<table border=\"1\" width=\"75%\" >\n";
		ostr << "<caption><b>General functions with two arguments</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";

		for(const auto& pair : g_funcs_gen_2args)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
Tobias WEBER's avatar
Tobias WEBER committed
367
368
369
	}


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
	if(g_funcs_gen_vararg.size())
	{
		ostr << "<table border=\"1\" width=\"75%\" >\n";
		ostr << "<caption><b>General functions with variable arguments</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Function" << "</th>" << "<th>" << "Description" << "</th>" << "</tr>\n";

		for(const auto& pair : g_funcs_gen_vararg)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" << "<td>" << std::get<1>(pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}

386
387
388
389
390
391
392
393
394
395
396
397
	return std::make_shared<SymbolString>(ostr.str());
}


/**
 * list of variables
 */
std::shared_ptr<Symbol> func_vars(CliParserContext & ctx)
{
	std::ostringstream ostr;

	// constants
Tobias WEBER's avatar
Tobias WEBER committed
398
	ostr << "<table border=\"1\" width=\"75%\" >\n";
399
400
401
402
403
	ostr << "<caption><b>Constants</b></caption>\n";
	ostr << "<tr>" << "<th>" << "Constant" << "</th>" 
		<< "<th>" << "Type" << "</th>" 
		<< "<th>" << "Value" << "</th>" 
		<< "<th>" << "Description" << "</th>" << "</tr>\n";
Tobias WEBER's avatar
Tobias WEBER committed
404
405
406
407

	for(const auto& pair : g_consts_real)
	{
		ostr << "<tr>";
408
409
410
411
		ostr << "<td>" << pair.first << "</td>" 
			<< "<td>" << "real" << "</td>"
			<< "<td>" << std::get<0>(pair.second) << "</td>" 
			<< "<td>" << std::get<1>(pair.second) << "</td>\n";
Tobias WEBER's avatar
Tobias WEBER committed
412
413
414
415
416
417
		ostr << "</tr>\n";
	}
	ostr << "</table>\n";
	ostr << "<br>\n";


418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441


	// variables
	if(auto *workspace = ctx.GetWorkspace(); workspace)
	{
		ostr << "<table border=\"1\" width=\"75%\" >\n";
		ostr << "<caption><b>Variables</b></caption>\n";
		ostr << "<tr>" << "<th>" << "Variable" << "</th>" 
			<< "<th>" << "Type" << "</th>" 
			<< "<th>" << "Value" << "</th>" << "</tr>\n";

		for(const auto& pair : *workspace)
		{
			ostr << "<tr>";
			ostr << "<td>" << pair.first << "</td>" 
				<< "<td>" << Symbol::get_type_name(*pair.second) << "</td>"
				<< "<td>" << (*pair.second) << "</td>\n";
			ostr << "</tr>\n";
		}
		ostr << "</table>\n";
		ostr << "<br>\n";
	}


Tobias WEBER's avatar
Tobias WEBER committed
442
443
444
445
	return std::make_shared<SymbolString>(ostr.str());
}


446
447
448
/**
 * clear all variables
 */
Tobias WEBER's avatar
Tobias WEBER committed
449
std::shared_ptr<Symbol> func_clear(CliParserContext &ctx)
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
{
	if(auto *workspace = ctx.GetWorkspace(); workspace)
	{
		std::ostringstream ostr;
		ostr << workspace->size() << " variables removed.";

		workspace->clear();
		ctx.EmitWorkspaceUpdated();

		return std::make_shared<SymbolString>(ostr.str());
	}

	return std::make_shared<SymbolString>("No workspace available.");
}



Tobias WEBER's avatar
Tobias WEBER committed
467
/**
468
 * append arrays or datasets
Tobias WEBER's avatar
Tobias WEBER committed
469
 */
470
std::shared_ptr<Symbol> func_append(CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
Tobias WEBER's avatar
Tobias WEBER committed
471
{
472
473
474
475
476
	if(args.size() == 0)
	{
		ctx.PrintError("No arguments given.");
		return nullptr;
	}
Tobias WEBER's avatar
Tobias WEBER committed
477
478


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
	// append datasets
	if(args[0]->GetType() == SymbolType::DATASET)
	{
		// first dataset
		Dataset datret = dynamic_cast<const SymbolDataset&>(*args[0]).GetValue();

		for(std::size_t idx=1; idx<args.size(); ++idx)
		{
			if(args[idx]->GetType() != SymbolType::DATASET)
			{
				ctx.PrintError("Mismatching argument types. Expected data sets.");
				return nullptr;
			}

			const auto& dat = dynamic_cast<const SymbolDataset&>(*args[idx]).GetValue();
			datret = Dataset::append(datret, dat);
		}

		return std::make_shared<SymbolDataset>(datret);
	}

	// append arrays
	else if(args[0]->GetType() == SymbolType::ARRAY)
	{
		std::vector<std::shared_ptr<Symbol>> arrret;

		for(std::size_t idx=0; idx<args.size(); ++idx)
		{
			if(args[idx]->GetType() != SymbolType::ARRAY)
			{
				ctx.PrintError("Mismatching argument types. Expected arrays.");
				return nullptr;
			}

			const auto& arr = dynamic_cast<const SymbolList&>(*args[idx]).GetValue();
			for(const auto& sym : arr)
				arrret.push_back(sym->copy());
		}

		return std::make_shared<SymbolList>(arrret, false);
	}


	// otherwise fail
	ctx.PrintError("Invalid argument type for append operation: ", Symbol::get_type_name(*args[0]), ".");
	return nullptr;
}

Tobias WEBER's avatar
Tobias WEBER committed
527

Tobias WEBER's avatar
Tobias WEBER committed
528

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/**
 * append datasets as the channels of a new dataset
 */
std::shared_ptr<Symbol> func_append_channels(CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
{
	if(args.size() == 0)
	{
		ctx.PrintError("No arguments given.");
		return nullptr;
	}


	// append datasets
	if(args[0]->GetType() == SymbolType::DATASET)
	{
		// first dataset
		Dataset datret = dynamic_cast<const SymbolDataset&>(*args[0]).GetValue();

		for(std::size_t idx=1; idx<args.size(); ++idx)
		{
			if(args[idx]->GetType() != SymbolType::DATASET)
			{
				ctx.PrintError("Mismatching argument types. Expected data sets.");
				return nullptr;
			}

			const auto& dat = dynamic_cast<const SymbolDataset&>(*args[idx]).GetValue();
			datret = Dataset::append_channels(datret, dat);
		}

		return std::make_shared<SymbolDataset>(datret);
	}


	// otherwise fail
	ctx.PrintError("Invalid argument type for append operation: ", Symbol::get_type_name(*args[0]), ".");
	return nullptr;
}



Tobias WEBER's avatar
Tobias WEBER committed
570
/**
571
 * point-wise addition of arrays or datasets
Tobias WEBER's avatar
Tobias WEBER committed
572
 */
573
std::shared_ptr<Symbol> func_add_pointwise(CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
Tobias WEBER's avatar
Tobias WEBER committed
574
{
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
	if(args.size() == 0)
	{
		ctx.PrintError("No arguments given.");
		return nullptr;
	}


	// add datasets
	if(args[0]->GetType() == SymbolType::DATASET)
	{
		// first dataset
		Dataset datret = dynamic_cast<const SymbolDataset&>(*args[0]).GetValue();

		for(std::size_t idx=1; idx<args.size(); ++idx)
		{
			if(args[idx]->GetType() != SymbolType::DATASET)
			{
				ctx.PrintError("Mismatching argument types. Expected data sets.");
				return nullptr;
			}

			const auto& dat = dynamic_cast<const SymbolDataset&>(*args[idx]).GetValue();
			datret = Dataset::add_pointwise(datret, dat);
		}

		return std::make_shared<SymbolDataset>(datret);
	}

Tobias WEBER's avatar
Tobias WEBER committed
603

604
605
606
607
608
609
610
	// otherwise simply call + operator
	std::shared_ptr<Symbol> symRet = std::make_shared<SymbolReal>(0);
	for(std::size_t idx=0; idx<args.size(); ++idx)
		symRet = Symbol::add(symRet, args[idx]);

	return symRet;
}
Tobias WEBER's avatar
Tobias WEBER committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627



/**
 * normalise dataset to monitor counter
 */
std::shared_ptr<Symbol> func_normtomon(CliParserContext & ctx, std::shared_ptr<Symbol> arg)
{
	if(!arg || arg->GetType() != SymbolType::DATASET)
	{
		ctx.PrintError("Expected a data set.");
		return nullptr;
	}

	const auto &dat = dynamic_cast<const SymbolDataset&>(*arg);
	return std::make_shared<SymbolDataset>(dat.GetValue().norm(0));
}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698



/**
 * fit function
 */
std::shared_ptr<Symbol> func_fit (CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
{
	if(args.size() < 2)
	{
		ctx.PrintError("Insufficient number of arguments given. At least a dataset and a fit function is needed.");
		return nullptr;
	}

	// TODO: multiple data sets
	if(args[0]->GetType() != SymbolType::DATASET)
	{
		ctx.PrintError("The first argument has to be a data set or a list of data sets.");
		return nullptr;
	}

	if(args[1]->GetType() != SymbolType::STRING)
	{
		ctx.PrintError("The second argument has to be a string giving the fit expression.");
		return nullptr;
	}


	const auto& dataset = dynamic_cast<SymbolDataset&>(*args[0]).GetValue();
	const std::string& strexpr = dynamic_cast<SymbolString&>(*args[1]).GetValue();

	std::vector<std::string> vars;
	std::vector<t_real> initials;
	std::vector<t_real> errs;
	std::vector<bool> fixed;


	// if no variables are given, determine them
	if(vars.size() == 0)
	{
		tl2::ExprParser<t_real> expr;
		expr.parse(strexpr);
		const auto& exprvars = expr.get_vars();
		for(const auto& pair : exprvars)
		{
			if(pair.first == "x")
				continue;
			
			vars.push_back(pair.first);
		}
	}

	initials.resize(vars.size(), 0);
	errs.resize(vars.size(), 0);
	fixed.resize(vars.size(), false);


	// fit all channels in the data set
	for(std::size_t channel=0; channel<dataset.GetNumChannels(); ++channel)
	{
		const auto& dat = dataset.GetChannel(channel);

		const auto& xvals = dat.GetAxis(0);
		const auto& yvals = dat.GetCounter(0);
		const auto& yerrs = dat.GetCounterErrors(0);

		tl2::fit_expr(strexpr, xvals, yvals, yerrs, "x", vars, initials, errs, &fixed);
	}

	return nullptr;
}
Tobias WEBER's avatar
Tobias WEBER committed
699
700
701
702
703
// ----------------------------------------------------------------------------




Tobias WEBER's avatar
Tobias WEBER committed
704
705
706
707




708
709
710

// ----------------------------------------------------------------------------
// maps
Tobias WEBER's avatar
Tobias WEBER committed
711
// ----------------------------------------------------------------------------
712

Tobias WEBER's avatar
Tobias WEBER committed
713
/**
714
 * map of real functions with one argument
Tobias WEBER's avatar
Tobias WEBER committed
715
 */
716
std::unordered_map<std::string, std::tuple<t_real_cli(*)(t_real), std::string>> g_funcs_real_1arg =
Tobias WEBER's avatar
Tobias WEBER committed
717
{
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
	std::make_pair("sin", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::sin), "")),
	std::make_pair("cos", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::cos), "")),
	std::make_pair("tan", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::tan), "")),
	std::make_pair("asin", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::asin), "")),
	std::make_pair("acos", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::acos), "")),
	std::make_pair("atan", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::atan), "")),

	std::make_pair("sinh", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::sinh), "")),
	std::make_pair("cosh", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::cosh), "")),
	std::make_pair("tanh", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::tanh), "")),
	std::make_pair("asinh", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::asinh), "")),
	std::make_pair("acosh", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::acosh), "")),
	std::make_pair("atanh", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::atanh), "")),

	std::make_pair("sqrt", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::sqrt), "")),
	std::make_pair("cbrt", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::cbrt), "")),

	std::make_pair("log", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::log), "")),
	std::make_pair("log10", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::log10), "")),
	std::make_pair("log2", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::log2), "")),
	std::make_pair("exp", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::exp), "")),
	std::make_pair("exp2", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::exp2), "")),

	std::make_pair("abs", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::abs), "")),
	std::make_pair("round", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::round), "")),
	std::make_pair("nearbyint", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::nearbyint), "")),
	std::make_pair("trunc", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::trunc), "")),
	std::make_pair("ceil", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::ceil), "")),
	std::make_pair("floor", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::floor), "")),

	std::make_pair("erf", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::erf), "")),
	std::make_pair("erfc", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::erfc), "")),
	//std::make_pair("beta", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::beta), "")),
	std::make_pair("gamma", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::tgamma), "")),
	std::make_pair("loggamma", std::make_tuple(static_cast<t_real(*)(t_real)>(&std::lgamma), "")),
};
Tobias WEBER's avatar
Tobias WEBER committed
754
755
756


/**
757
 * map of real functions with two arguments
Tobias WEBER's avatar
Tobias WEBER committed
758
 */
759
std::unordered_map<std::string, std::tuple<t_real_cli(*)(t_real, t_real), std::string>> g_funcs_real_2args =
Tobias WEBER's avatar
Tobias WEBER committed
760
{
761
	std::make_pair("pow", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::pow), "")),
Tobias WEBER's avatar
Tobias WEBER committed
762

763
764
	std::make_pair("atan2", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::atan2), "")),
	std::make_pair("hypot", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::hypot), "")),
Tobias WEBER's avatar
Tobias WEBER committed
765

766
767
768
769
770
771
772
773
774
	std::make_pair("max", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::fmax), "")),
	std::make_pair("min", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::fmin), "")),
	//std::make_pair("diff", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::fdim), "")),

	std::make_pair("remainder", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::remainder), "")),
	std::make_pair("mod", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::fmod), "")),

	std::make_pair("copysign", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::copysign), "")),
};
Tobias WEBER's avatar
Tobias WEBER committed
775
776
777
778



/**
779
 * map of array functions with one argument
Tobias WEBER's avatar
Tobias WEBER committed
780
 */
781
std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(std::shared_ptr<SymbolList>), std::string>> g_funcs_arr_1arg =
Tobias WEBER's avatar
Tobias WEBER committed
782
{
783
784
785
786
787
788
789
790
791
792
793
	std::make_pair("norm", std::make_tuple(&func_norm, "Euclidian norm")),
};

/**
 * map of array functions with two arguments
 */
std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(std::shared_ptr<SymbolList>, std::shared_ptr<SymbolList>), std::string>> g_funcs_arr_2args =
{
	std::make_pair("dot", std::make_tuple(&func_dot, "inner product")),
	std::make_pair("cross", std::make_tuple(&func_cross, "cross product")),
};
Tobias WEBER's avatar
Tobias WEBER committed
794
795


796
797
798
799
800
801
802
803
804
805
806
807
808

/**
 * map of general functions with zero arguments
 */
std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
	(CliParserContext&), std::string>> g_funcs_gen_0args =
{
	std::make_pair("help", std::make_tuple(&func_help, "display help")),
	std::make_pair("vars", std::make_tuple(&func_vars, "list variables")),
	std::make_pair("funcs", std::make_tuple(&func_funcs, "list functions")),
	std::make_pair("clear", std::make_tuple(&func_clear, "remove all variables")),
};

Tobias WEBER's avatar
Tobias WEBER committed
809
810
811
/**
 * map of general functions with one argument
 */
812
813
std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
	(CliParserContext&, std::shared_ptr<Symbol>), std::string>> g_funcs_gen_1arg =
Tobias WEBER's avatar
Tobias WEBER committed
814
{
815
816
817
	std::make_pair("typeof", std::make_tuple(&func_typeof, "return symbol type")),
	std::make_pair("sizeof", std::make_tuple(&func_sizeof, "return symbol size")),
	std::make_pair("toarray", std::make_tuple(&func_array, "convert a dataset to an array")),
Tobias WEBER's avatar
Tobias WEBER committed
818
819

	std::make_pair("normtomon", std::make_tuple(&func_normtomon, "normalise data set to monitor counter")),
Tobias WEBER's avatar
Tobias WEBER committed
820
821
822
823
824
};

/**
 * map of general functions with two arguments
 */
825
826
std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
	(CliParserContext&, std::shared_ptr<Symbol>, std::shared_ptr<Symbol>), std::string>> g_funcs_gen_2args =
Tobias WEBER's avatar
Tobias WEBER committed
827
828
{
};
Tobias WEBER's avatar
Tobias WEBER committed
829

830
831
832
833
834
835
836
/**
 * map of general functions with variable arguments
 */
std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
	(CliParserContext&, const std::vector<std::shared_ptr<Symbol>>&), std::string>> g_funcs_gen_vararg = 
{
	std::make_pair("append", std::make_tuple(&func_append, "appends two or more data sets")),
837
	std::make_pair("append_channels", std::make_tuple(&func_append_channels, "appends two or more data set as individual channels")),
838
	std::make_pair("add_pointwise", std::make_tuple(&func_add_pointwise, "pointwise addition of two or more data sets")),
839
	std::make_pair("fit", std::make_tuple(&func_fit, "fitting of one or more data sets")),
840
};
Tobias WEBER's avatar
Tobias WEBER committed
841
842
843
844
845



/**
 * map of real constants
Tobias WEBER's avatar
Tobias WEBER committed
846
 */
Tobias WEBER's avatar
Tobias WEBER committed
847
std::unordered_map<std::string, std::tuple<t_real_cli, std::string>> g_consts_real
Tobias WEBER's avatar
Tobias WEBER committed
848
{
849
	std::make_pair("pi", std::make_tuple(tl2::pi<t_real>, "pi")),
Tobias WEBER's avatar
Tobias WEBER committed
850
851
};
// ----------------------------------------------------------------------------