funcs.cpp 24 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
/**
 * Built-in functions
 * @author Tobias Weber <tweber@ill.fr>
 * @date 20-Jun-2018
 * @license see 'LICENSE' file
 */

#include "funcs.h"
Tobias WEBER's avatar
Tobias WEBER committed
9
#include "../globals.h"
10
#include "libs/phys.h"
11
#include "libs/fit.h"
Tobias WEBER's avatar
Tobias WEBER committed
12
13
14
15
16


using t_real = t_real_cli;


Tobias WEBER's avatar
Tobias WEBER committed
17
// ----------------------------------------------------------------------------
18
19
// real functions
// ----------------------------------------------------------------------------
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

/**
 * 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
52
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
53
54
55



Tobias WEBER's avatar
Tobias WEBER committed
56
57


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

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * 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
119
// ----------------------------------------------------------------------------
120

Tobias WEBER's avatar
Tobias WEBER committed
121
122
123
/**
 * typeof function
 */
Tobias WEBER's avatar
Tobias WEBER committed
124
static std::shared_ptr<Symbol> func_typeof(CliParserContext&, std::shared_ptr<Symbol> sym)
Tobias WEBER's avatar
Tobias WEBER committed
125
126
127
128
129
{
	const std::string& ty = Symbol::get_type_name(*sym);
	return std::make_shared<SymbolString>(ty);
}

Tobias WEBER's avatar
Tobias WEBER committed
130

131
132
133
/**
 * sizeof function
 */
Tobias WEBER's avatar
Tobias WEBER committed
134
static std::shared_ptr<Symbol> func_sizeof(CliParserContext&, std::shared_ptr<Symbol> sym)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
	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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
/**
 * 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
208
209
210
/**
 * help
 */
Tobias WEBER's avatar
Tobias WEBER committed
211
std::shared_ptr<Symbol> func_help(CliParserContext&)
Tobias WEBER's avatar
Tobias WEBER committed
212
213
214
{
	std::ostringstream ostr;

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

218
219
220
221
222
223
224
225
226
227
	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
228
std::shared_ptr<Symbol> func_funcs(CliParserContext&)
229
230
231
{
	std::ostringstream ostr;

Tobias WEBER's avatar
Tobias WEBER committed
232

233
	if(g_funcs_real_1arg.size())
Tobias WEBER's avatar
Tobias WEBER committed
234
	{
235
236
237
		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
238

239
240
241
242
243
244
245
246
247
		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
248
249


250
	if(g_funcs_real_2args.size())
Tobias WEBER's avatar
Tobias WEBER committed
251
	{
252
253
254
		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
255

256
257
258
259
260
261
262
263
264
		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
265
266


267
	if(g_funcs_arr_1arg.size())
Tobias WEBER's avatar
Tobias WEBER committed
268
	{
269
270
271
		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
272

273
274
275
276
277
278
279
280
281
		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
282
283


284
	if(g_funcs_arr_2args.size())
Tobias WEBER's avatar
Tobias WEBER committed
285
	{
286
287
288
		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
289

290
291
292
293
294
295
296
297
298
		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
299
300


301
	if(g_funcs_gen_0args.size())
Tobias WEBER's avatar
Tobias WEBER committed
302
	{
303
304
305
		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
306

307
308
309
310
311
312
313
314
315
		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
316
317


318
	if(g_funcs_gen_1arg.size())
Tobias WEBER's avatar
Tobias WEBER committed
319
	{
320
321
322
		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
323

324
325
326
327
328
329
330
331
332
		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
333
334


335
	if(g_funcs_gen_2args.size())
Tobias WEBER's avatar
Tobias WEBER committed
336
	{
337
338
339
340
341
342
343
344
345
346
347
348
		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
349
350
351
	}


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	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";
	}

368
369
370
371
372
373
374
375
376
377
378
379
	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
380
	ostr << "<table border=\"1\" width=\"75%\" >\n";
381
382
383
384
385
	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
386
387
388
389

	for(const auto& pair : g_consts_real)
	{
		ostr << "<tr>";
390
391
392
393
		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
394
395
396
397
398
399
		ostr << "</tr>\n";
	}
	ostr << "</table>\n";
	ostr << "<br>\n";


400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423


	// 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
424
425
426
427
	return std::make_shared<SymbolString>(ostr.str());
}


428
429
430
/**
 * clear all variables
 */
Tobias WEBER's avatar
Tobias WEBER committed
431
std::shared_ptr<Symbol> func_clear(CliParserContext &ctx)
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
{
	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
449
/**
450
 * append arrays or datasets
Tobias WEBER's avatar
Tobias WEBER committed
451
 */
452
std::shared_ptr<Symbol> func_append(CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
Tobias WEBER's avatar
Tobias WEBER committed
453
{
454
455
456
457
458
	if(args.size() == 0)
	{
		ctx.PrintError("No arguments given.");
		return nullptr;
	}
Tobias WEBER's avatar
Tobias WEBER committed
459
460


461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
	// 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
509

Tobias WEBER's avatar
Tobias WEBER committed
510

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
 * 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
552
/**
553
 * point-wise addition of arrays or datasets
Tobias WEBER's avatar
Tobias WEBER committed
554
 */
555
std::shared_ptr<Symbol> func_add_pointwise(CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
Tobias WEBER's avatar
Tobias WEBER committed
556
{
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
	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
585

586
587
588
589
590
591
592
	// 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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609



/**
 * 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));
}
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
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



/**
 * 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
681
682
683
684
685
// ----------------------------------------------------------------------------




Tobias WEBER's avatar
Tobias WEBER committed
686
687
688
689




690
691
692

// ----------------------------------------------------------------------------
// maps
Tobias WEBER's avatar
Tobias WEBER committed
693
// ----------------------------------------------------------------------------
694

Tobias WEBER's avatar
Tobias WEBER committed
695
/**
696
 * map of real functions with one argument
Tobias WEBER's avatar
Tobias WEBER committed
697
 */
698
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
699
{
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
	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
736
737
738


/**
739
 * map of real functions with two arguments
Tobias WEBER's avatar
Tobias WEBER committed
740
 */
741
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
742
{
743
	std::make_pair("pow", std::make_tuple(static_cast<t_real(*)(t_real, t_real)>(&std::pow), "")),
Tobias WEBER's avatar
Tobias WEBER committed
744

745
746
	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
747

748
749
750
751
752
753
754
755
756
	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
757
758
759
760



/**
761
 * map of array functions with one argument
Tobias WEBER's avatar
Tobias WEBER committed
762
 */
763
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
764
{
765
766
767
768
769
770
771
772
773
774
775
	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
776
777


778
779
780
781
782
783
784
785
786
787
788
789
790

/**
 * 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
791
792
793
/**
 * map of general functions with one argument
 */
794
795
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
796
{
797
798
799
	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
800
801

	std::make_pair("normtomon", std::make_tuple(&func_normtomon, "normalise data set to monitor counter")),
Tobias WEBER's avatar
Tobias WEBER committed
802
803
804
805
806
};

/**
 * map of general functions with two arguments
 */
807
808
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
809
810
{
};
Tobias WEBER's avatar
Tobias WEBER committed
811

812
813
814
815
816
817
818
/**
 * 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")),
819
	std::make_pair("append_channels", std::make_tuple(&func_append_channels, "appends two or more data set as individual channels")),
820
	std::make_pair("add_pointwise", std::make_tuple(&func_add_pointwise, "pointwise addition of two or more data sets")),
821
	std::make_pair("fit", std::make_tuple(&func_fit, "fitting of one or more data sets")),
822
};
Tobias WEBER's avatar
Tobias WEBER committed
823
824
825
826
827



/**
 * map of real constants
Tobias WEBER's avatar
Tobias WEBER committed
828
 */
Tobias WEBER's avatar
Tobias WEBER committed
829
std::unordered_map<std::string, std::tuple<t_real_cli, std::string>> g_consts_real
Tobias WEBER's avatar
Tobias WEBER committed
830
{
831
	std::make_pair("pi", std::make_tuple(tl2::pi<t_real>, "pi")),
Tobias WEBER's avatar
Tobias WEBER committed
832
833
};
// ----------------------------------------------------------------------------