Loading...
Searching...
No Matches
operators_example.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25// ================================================================================== //
26// OPERATORS - EXAMPLES OF USAGE - //
27// //
28// Examples of Operators usage. //
29// ================================================================================== //
30// INFO //
31// ================================================================================== //
32// Author : Alessandro Alaia //
33// Version : v3.0 //
34// //
35// All rights reserved. //
36// ================================================================================== //
37
40// ================================================================================== //
41// INCLUDES //
42// ================================================================================== //
43# include "operators_example.hpp"
44
45// ================================================================================== //
46// IMPLEMENTATIONS //
47// ================================================================================== //
48
49// ---------------------------------------------------------------------------------- //
50void vectorOperators_Ex(
51 void
52) {
53
54// ================================================================================== //
55// void vectorOperators_Ex( //
56// void) //
57// //
58// Examples of usage of operators for vectors //
59// ================================================================================== //
60// INPUT //
61// ================================================================================== //
62// - none //
63// ================================================================================== //
64// OUTPUT //
65// ================================================================================== //
66// - none //
67// ================================================================================== //
68
69// ================================================================================== //
70// VARIABLES DECLARATION //
71// ================================================================================== //
72
73// Local variables
74string selection;
75
76// Counters
77// none
78
79// ================================================================================== //
80// OUTPUT MESSAGE //
81// ================================================================================== //
82{
83 // Select sub case
84 cout << "DEMO: OPERATORS FOR STL VECTORS" << endl;
85 cout << " Select operators: " << endl;
86 cout << " ""+"" operator" << endl;
87 cout << " ""-"" operator" << endl;
88 cout << " ""*"" operator" << endl;
89 cout << " ""/"" operator" << endl;
90 cin >> selection;
91}
92
93// ================================================================================== //
94// OPERATOR "+" //
95// ================================================================================== //
96if (selection.compare("+") == 0) {
97
98 // Scope variables
99 double a = 1, b = 2;
100 dvector1D x, y, z;
101 dvector2D X, Y, Z;
102
103 // Initialize vectors
104 x.resize(3, 1.0);
105 x[0] = 1.0; x[1] = 2.0; x[2] = 3.0;
106 y = x;
107 X.resize(3, dvector1D(2, 0.0));
108 X[0][0] = 1.0; X[0][1] = 2.0;
109 X[1][0] = 3.0; X[1][1] = 4.0;
110 X[2][0] = 5.0; X[2][1] = 6.0;
111 Y = X;
112
113 // Variables
114 cout << "a = " << a << endl;
115 cout << "b = " << b << endl;
116 cout << "x = ";
117 cout << x << endl;
118 cout << "y = ";
119 cout << y << endl;
120 cout << "X = ";
121 cout << X << endl;
122 cout << "Y = ";
123 cout << Y << endl;
124
125 // Sum between vectors
126 cout << "sum between 1D vectors --------------------------- " << endl;
127 cout << "x + y = ";
128 z = x+y;
129 cout << z << endl;
130 cout << "sum between 2D vectors --------------------------- " << endl;
131 cout << "X + Y = ";
132 Z = X + Y;
133 cout << Z << endl;
134 cout << "sum between 1D/2D vectors ------------------------ " << endl;
135 cout << "x + Y = ";
136 Z = x + Y;
137 cout << Z << endl;
138 cout << "X + y = ";
139 Z = X + y;
140 cout << Z << endl;
141 cout << "sum between const and 1D vectors ----------------- " << endl;
142 cout << "a + x = ";
143 z = a + x;
144 cout << z << endl;
145 cout << "y + b = ";
146 z = y + b;
147 cout << z << endl;
148 cout << "sum between const and 2D vectors ----------------- " << endl;
149 cout << "a + X = ";
150 Z = a + X;
151 cout << Z << endl;
152 cout << "Y + b = ";
153 Z = Y + b;
154 cout << Z << endl;
155}
156
157// ================================================================================== //
158// OPERATOR "-" //
159// ================================================================================== //
160else if (selection.compare("-") == 0) {
161
162 // Scope variables
163 double a = 1, b = 2;
164 dvector1D x, y, z;
165 dvector2D X, Y, Z;
166
167 // Initialize vectors
168 x.resize(3, 1.0);
169 x[0] = 1.0; x[1] = 2.0; x[2] = 3.0;
170 y = x;
171 X.resize(3, dvector1D(2, 0.0));
172 X[0][0] = 1.0; X[0][1] = 2.0;
173 X[1][0] = 3.0; X[1][1] = 4.0;
174 X[2][0] = 5.0; X[2][1] = 6.0;
175 Y = X;
176
177 // Variables
178 cout << "a = " << a << endl;
179 cout << "b = " << b << endl;
180 cout << "x = ";
181 cout << x << endl;
182 cout << "y = ";
183 cout << y << endl;
184 cout << "X = ";
185 cout << X << endl;
186 cout << "Y = ";
187 cout << Y << endl;
188
189 // Sum between vectors
190 cout << "diff between 1D vectors --------------------------- " << endl;
191 cout << "x - y = ";
192 z = x - y;
193 cout << z << endl;
194 cout << "diff between 2D vectors --------------------------- " << endl;
195 cout << "X - Y = ";
196 Z = X - Y;
197 cout << Z << endl;
198 cout << "diff between 1D/2D vectors ------------------------ " << endl;
199 cout << "x - Y = ";
200 Z = x - Y;
201 cout << Z << endl;
202 cout << "X - y = ";
203 Z = X - y;
204 cout << Z << endl;
205 cout << "diff between const and 1D vectors ----------------- " << endl;
206 cout << "a - x = ";
207 z = a - x;
208 cout << z << endl;
209 cout << "y - b = ";
210 z = y - b;
211 cout << z << endl;
212 cout << "diff between const and 2D vectors ----------------- " << endl;
213 cout << "a - X = ";
214 Z = a - X;
215 cout << Z << endl;
216 cout << "Y - b = ";
217 Z = Y - b;
218 cout << Z << endl;
219}
220
221// ================================================================================== //
222// OPERATOR "*" //
223// ================================================================================== //
224else if (selection.compare("*") == 0) {
225
226 // Scope variables
227 double a = 1, b = 2;
228 dvector1D x, y, z;
229 dvector2D X, Y, Z;
230
231 // Initialize vectors
232 x.resize(3, 1.0);
233 x[0] = 1.0; x[1] = 2.0; x[2] = 3.0;
234 y = x;
235 X.resize(3, dvector1D(2, 0.0));
236 X[0][0] = 1.0; X[0][1] = 2.0;
237 X[1][0] = 3.0; X[1][1] = 4.0;
238 X[2][0] = 5.0; X[2][1] = 6.0;
239 Y = X;
240
241 // Variables
242 cout << "a = " << a << endl;
243 cout << "b = " << b << endl;
244 cout << "x = ";
245 cout << x << endl;
246 cout << "y = ";
247 cout << y << endl;
248 cout << "X = ";
249 cout << X << endl;
250 cout << "Y = ";
251 cout << Y << endl;
252
253 // Sum between vectors
254 cout << "prod between 1D vectors --------------------------- " << endl;
255 cout << "x * y = ";
256 z = x * y;
257 cout << z << endl;
258 cout << "prod between 2D vectors --------------------------- " << endl;
259 cout << "X * Y = ";
260 Z = X * Y;
261 cout << Z << endl;
262 cout << "prod between 1D/2D vectors ------------------------ " << endl;
263 cout << "x * Y = ";
264 Z = x * Y;
265 cout << Z << endl;
266 cout << "X * y = ";
267 Z = X * y;
268 cout << Z << endl;
269 cout << "prod between const and 1D vectors ----------------- " << endl;
270 cout << "a * x = ";
271 z = a * x;
272 cout << z << endl;
273 cout << "y * b = ";
274 z = y * b;
275 cout << z << endl;
276 cout << "prod between const and 2D vectors ----------------- " << endl;
277 cout << "a * X = ";
278 Z = a * X;
279 cout << Z << endl;
280 cout << "Y * b = ";
281 Z = Y * b;
282 cout << Z << endl;
283}
284
285// ================================================================================== //
286// OPERATOR "/" //
287// ================================================================================== //
288else if (selection.compare("/") == 0) {
289
290 // Scope variables
291 double a = 1, b = 2;
292 dvector1D x, y, z;
293 dvector2D X, Y, Z;
294
295 // Initialize vectors
296 x.resize(3, 1.0);
297 x[0] = 1.0; x[1] = 2.0; x[2] = 3.0;
298 y = x;
299 X.resize(3, dvector1D(2, 0.0));
300 X[0][0] = 1.0; X[0][1] = 2.0;
301 X[1][0] = 3.0; X[1][1] = 4.0;
302 X[2][0] = 5.0; X[2][1] = 6.0;
303 Y = X;
304
305 // Variables
306 cout << "a = " << a << endl;
307 cout << "b = " << b << endl;
308 cout << "x = " << endl;
309 cout << x << endl;
310 cout << "y = ";
311 cout << y << endl;
312 cout << "X = ";
313 cout << X << endl;
314 cout << "Y = ";
315 cout << Y << endl;
316
317 // Sum between vectors
318 cout << "div between 1D vectors --------------------------- " << endl;
319 cout << "x / y = ";
320 z = x / y;
321 cout << z << endl;
322 cout << "div between 2D vectors --------------------------- " << endl;
323 cout << "X / Y = ";
324 Z = X / Y;
325 cout << Z << endl;
326 cout << "div between 1D/2D vectors ------------------------ " << endl;
327 cout << "x / Y = ";
328 Z = x / Y;
329 cout << Z << endl;
330 cout << "X / y = ";
331 Z = X / y;
332 cout << Z << endl;
333 cout << "div between const and 1D vectors ----------------- " << endl;
334 cout << "a / x = ";
335 z = a / x;
336 cout << z << endl;
337 cout << "y / b = ";
338 z = y / b;
339 cout << z << endl;
340 cout << "div between const and 2D vectors ----------------- " << endl;
341 cout << "a / X = ";
342 Z = a / X;
343 cout << Z << endl;
344 cout << "Y / b = ";
345 Z = Y / b;
346 cout << Z << endl;
347}
348
349// ================================================================================== //
350// OPERATOR "not available" //
351// ================================================================================== //
352else {
353 cout << " Operator not available!" << endl;
354}
355
356// ================================================================================== //
357// OUTPUT MESSAGE //
358// ================================================================================== //
359{
360 cout << "DEMO: done!!" << endl;
361}
362
363return; };
364
365// ---------------------------------------------------------------------------------- //
366void vectorMathFunct_Ex(
367 void
368) {
369
370// ================================================================================== //
371// void vectorMathFunct_Ex( //
372// void) //
373// //
374// Examples of usage of operators for vectors //
375// ================================================================================== //
376// INPUT //
377// ================================================================================== //
378// - none //
379// ================================================================================== //
380// OUTPUT //
381// ================================================================================== //
382// - none //
383// ================================================================================== //
384
385// ================================================================================== //
386// VARIABLES DECLARATION //
387// ================================================================================== //
388
389// Local variables
390string selection;
391
392// Counters
393// none
394
395// ================================================================================== //
396// OUTPUT MESSAGE //
397// ================================================================================== //
398{
399 // Select sub case
400 cout << "DEMO: MATH FUNCTIONS FOR STL VECTORS" << endl;
401 cout << " Select operators: " << endl;
402 cout << " 'min' operator" << endl;
403 cout << " 'max' operator" << endl;
404 cout << " 'minval' operator" << endl;
405 cout << " 'maxval' operator" << endl;
406 cout << " 'sum' operator" << endl;
407 cout << " 'abs' operator" << endl;
408 cout << " 'pow' operator" << endl;
409 cout << " 'norm' operator" << endl;
410 cout << " 'dotProduct' operator" << endl;
411 cout << " 'crossProduct' operator" << endl;
412 cin >> selection;
413
414}
415
416// ================================================================================== //
417// OPERATOR "min" //
418// ================================================================================== //
419if (selection.compare("min") == 0) {
420
421 // Scope variables -------------------------------------------------------------- //
422 double a = 1.5;
423 dvector1D x(3), t(3);
424 dvector2D y(3, dvector1D(3)), z(3, dvector1D(3));
425
426 // Initialize scope variables --------------------------------------------------- //
427 x[0] = 3.0; x[1] = 3.0; x[2] = 3.0;
428 t[0] = 1.0; t[1] = 4.0; t[2] = 2.0;
429 y[0][0] = 0.0; y[0][1] = 1.0; y[0][2] = 2.0;
430 y[1][0] = 3.0; y[1][1] = 4.0; y[1][2] = 5.0;
431 y[2][0] = 6.0; y[2][1] = 7.0; y[2][2] = 8.0;
432 z[0][0] = 2.0; z[0][1] = 2.0; z[0][2] = 2.0;
433 z[1][0] = 2.0; z[1][1] = 2.0; z[1][2] = 2.0;
434 z[2][0] = 2.0; z[2][1] = 2.0; z[2][2] = 2.0;
435 cout << "a = " << a << endl;
436 cout << "x = " << x << endl;
437 cout << "t = " << t << endl;
438 cout << "y = " << y << endl;
439 cout << "z = " << z << endl;
440
441 // min between scalar and 1D array ---------------------------------------------- //
442 cout << "min(t, a) = " << min(t,a) << endl;
443
444 // min between 1D arrays -------------------------------------------------------- //
445 cout << "min(x, t) = " << min(x, t) << endl;
446
447 // min between scalar and 2D array ---------------------------------------------- //
448 cout << "min(y, a) = " << min(y, a) << endl;
449
450 // min between 1D array and 2D array -------------------------------------------- //
451 cout << "min(y, x) = " << min(y, x) << endl;
452
453 // min between 2D arrays -------------------------------------------------------- //
454 cout << "min(y, z) = " << min(y, z) << endl;
455
456}
457
458// ================================================================================== //
459// OPERATOR "minval" //
460// ================================================================================== //
461if (selection.compare("minval") == 0) {
462
463 // Scope variables -------------------------------------------------------------- //
464 int nn = 0;
465 double a = 3.0, mm;
466 dvector1D x(3, 0.0);
467 ivector1D i(3, 1);
468 ivector2D y(3, ivector1D(3, 0));
469
470 // Initialize variables --------------------------------------------------------- //
471 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
472 i[0] = 4; i[1] = 1; i[2] = 4;
473 y[0] = 2*i;
474 y[1] = i;
475 y[2] = 2*i;
476
477 // Output message --------------------------------------------------------------- //
478 cout << "a = " << a << endl;
479 cout << "x = " << x << endl;
480 cout << "y = " << y << endl;
481
482 // Minval of double ------------------------------------------------------------- //
483 minval(a,mm);
484 cout << "minval(a) = " << mm << endl;
485
486 // Minval of 1D vector ---------------------------------------------------------- //
487 minval(x,mm);
488 cout << "minval(x) = " << mm << endl;
489
490 // Minval of 2D vector ---------------------------------------------------------- //
491 minval(y,nn);
492 cout << "minval(y) = " << nn << endl;
493
494}
495
496// ================================================================================== //
497// OPERATOR "max" //
498// ================================================================================== //
499else if (selection.compare("max") == 0) {
500
501 // Scope variables -------------------------------------------------------------- //
502 double a = 1.5;
503 dvector1D x(3), t(3);
504 dvector2D y(3, dvector1D(3)), z(3, dvector1D(3));
505
506 // Initialize scope variables --------------------------------------------------- //
507 x[0] = 3.0; x[1] = 3.0; x[2] = 3.0;
508 t[0] = 1.0; t[1] = 4.0; t[2] = 2.0;
509 y[0][0] = 0.0; y[0][1] = 1.0; y[0][2] = 2.0;
510 y[1][0] = 3.0; y[1][1] = 4.0; y[1][2] = 5.0;
511 y[2][0] = 6.0; y[2][1] = 7.0; y[2][2] = 8.0;
512 z[0][0] = 2.0; z[0][1] = 2.0; z[0][2] = 2.0;
513 z[1][0] = 2.0; z[1][1] = 2.0; z[1][2] = 2.0;
514 z[2][0] = 2.0; z[2][1] = 2.0; z[2][2] = 2.0;
515 cout << "a = " << a << endl;
516 cout << "x = " << x << endl;
517 cout << "t = " << t << endl;
518 cout << "y = " << y << endl;
519 cout << "z = " << z << endl;
520
521 // min between scalar and 1D array ---------------------------------------------- //
522 cout << "max(t, a) = " << max(t,a) << endl;
523
524 // min between 1D arrays -------------------------------------------------------- //
525 cout << "max(x, t) = " << max(x, t) << endl;
526
527 // min between scalar and 2D array ---------------------------------------------- //
528 cout << "max(y, a) = " << max(y, a) << endl;
529
530 // min between 1D array and 2D array -------------------------------------------- //
531 cout << "max(y, x) = " << max(y, x) << endl;
532
533 // min between 2D arrays -------------------------------------------------------- //
534 cout << "max(y, z) = " << max(y, z) << endl;
535
536}
537
538// ================================================================================== //
539// OPERATOR "maxval" //
540// ================================================================================== //
541if (selection.compare("maxval") == 0) {
542
543 // Scope variables -------------------------------------------------------------- //
544 int nn = 0;
545 double a = 3.0, mm;
546 dvector1D x(3, 0.0);
547 ivector1D i(3, 1);
548 ivector2D y(3, ivector1D(3, 0));
549
550 // Initialize variables --------------------------------------------------------- //
551 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
552 i[0] = 1; i[1] = 2; i[2] = 4;
553 y[0] = i;
554 y[1] = 2*i;
555 y[2] = 3*i;
556
557 // Output message --------------------------------------------------------------- //
558 cout << "a = " << a << endl;
559 cout << "x = " << x << endl;
560 cout << "y = " << y << endl;
561
562 // Minval of double ------------------------------------------------------------- //
563 maxval(a,mm);
564 cout << "maxval(a) = " << mm << endl;
565
566 // Minval of 1D vector ---------------------------------------------------------- //
567 maxval(x,mm);
568 cout << "maxval(x) = " << mm << endl;
569
570 // Minval of 2D vector ---------------------------------------------------------- //
571 maxval(y,nn);
572 cout << "maxval(y) = " << nn << endl;
573
574}
575
576// ================================================================================== //
577// OPERATOR "sum" //
578// ================================================================================== //
579else if (selection.compare("sum") == 0) {
580
581 // Scope variables -------------------------------------------------------------- //
582 int nn = -5;
583 double a = 3.0, mm;
584 dvector1D x(3, 0.0);
585
586 uint32_t su;
587 vector<uint32_t> u(3, 0);
588 ivector1D i(3, 1);
589 ivector2D y(3, ivector1D(3, 0));
590
591 // Initialize variables --------------------------------------------------------- //
592 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
593 u[0] = 1; u[1] = 2; u[2] = 3 ;
594 i[0] = 1; i[1] = 2; i[2] = 4;
595 y[0] = i;
596 y[1] = 2*i;
597 y[2] = 3*i;
598
599 // Output message --------------------------------------------------------------- //
600 cout << "a = " << a << endl;
601 cout << "x = " << x << endl;
602 cout << "u = " << x << endl;
603 cout << "y = " << y << endl;
604
605 // Minval of double ------------------------------------------------------------- //
606 sum(a,mm);
607 cout << "sum(a) = " << mm << endl;
608
609 // Minval of 1D vector ---------------------------------------------------------- //
610 sum(x,mm);
611 cout << "sum(x) = " << mm << endl;
612
613 // Minval of 1D vector ---------------------------------------------------------- //
614 sum(u,su);
615 cout << "sum(u) = " << su << endl;
616
617
618 // Minval of 2D vector ---------------------------------------------------------- //
619 sum(y,nn);
620 cout << "sum(y) = " << nn << endl;
621
622
623}
624
625// ================================================================================== //
626// OPERATOR "abs" //
627// ================================================================================== //
628else if (selection.compare("abs") == 0) {
629
630 // Scope variables -------------------------------------------------------------- //
631 double a = 3.0;
632 dvector1D x(3, 0.0);
633 ivector1D i(3, 1);
634 ivector2D y(3, ivector1D(3, 0));
635
636 // Initialize variables --------------------------------------------------------- //
637 x[0] = 1.05; x[1] = -1.1; x[2] = 3.0;
638 i[0] = 1; i[1] = 2; i[2] = 4;
639 y[0] = i;
640 y[1] = 2*i;
641 y[2] = -1*i;
642
643 // Output message --------------------------------------------------------------- //
644 cout << "a = " << a << endl;
645 cout << "x = " << x << endl;
646 cout << "y = " << y << endl;
647
648 // Minval of double ------------------------------------------------------------- //
649 cout << "abs(a) = " << abs(a) << endl;
650
651 // Minval of 1D vector ---------------------------------------------------------- //
652 cout << "abs(x) = " << abs(x) << endl;
653
654 // Minval of 2D vector ---------------------------------------------------------- //
655 cout << "abs(y) = " << abs(y) << endl;
656
657}
658
659// ================================================================================== //
660// OPERATOR "pow" //
661// ================================================================================== //
662else if (selection.compare("pow") == 0) {
663
664 // Scope variables -------------------------------------------------------------- //
665 double a = 3.0;
666 dvector1D x(3, 0.0);
667 ivector1D i(3, 1);
668 ivector2D y(3, ivector1D(3, 0));
669
670 // Initialize variables --------------------------------------------------------- //
671 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
672 i[0] = 1; i[1] = 2; i[2] = 4;
673 y[0] = i;
674 y[1] = 2*i;
675 y[2] = 3*i;
676
677 // Output message --------------------------------------------------------------- //
678 cout << "a = " << a << endl;
679 cout << "x = " << x << endl;
680 cout << "y = " << y << endl;
681
682 // Minval of double ------------------------------------------------------------- //
683 cout << "pow(a,2) = " << pow(a,2.0) << endl;
684
685 // Minval of 1D vector ---------------------------------------------------------- //
686 cout << "pow(x,2) = " << pow(x,2.0) << endl;
687
688 // Minval of 2D vector ---------------------------------------------------------- //
689 cout << "pow(y,2) = " << pow(y,2.0) << endl;
690
691}
692
693// ================================================================================== //
694// OPERATOR "norm" //
695// ================================================================================== //
696else if (selection.compare("norm") == 0) {
697
698 // Scope variables -------------------------------------------------------------- //
699 dvector1D x(3, 0.0);
700 ivector1D i(3, 1);
701
702 // Initialize variables --------------------------------------------------------- //
703 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
704 i[0] = 1; i[1] = -2; i[2] = -4;
705
706 // Output message --------------------------------------------------------------- //
707 cout << "x = " << x << endl;
708 cout << "i = " << i << endl;
709
710 // norm of vector< double > ----------------------------------------------------- //
711 cout << "norm(x,2) = " << norm(x,2) << endl;
712 cout << "norm(x,3) = " << norm(x,3) << endl;
713 cout << "normInf(x) = " << normInf(x) << endl;
714
715 // norm of vector< int > -------------------------------------------------------- //
716 cout << "norm(i,2) = " << norm(i,2) << endl;
717 cout << "norm(i,3) = " << norm(i,3) << endl;
718 cout << "normInf(i) = " << normInf(i) << endl;
719
720}
721
722// ================================================================================== //
723// OPERATOR "dotProduct" //
724// ================================================================================== //
725else if (selection.compare("dotProduct") == 0) {
726
727 // Scope variables -------------------------------------------------------------- //
728 dvector1D x(3, 0.0), y(3, 0.0);
729 ivector1D i(3, 1), j(3, 0);
730
731 // Initialize variables --------------------------------------------------------- //
732 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
733 y = x;
734 i[0] = 1; i[1] = -2; i[2] = 4;
735 j = i;
736
737 // Output message --------------------------------------------------------------- //
738 cout << "x = " << x << endl;
739 cout << "y = " << x << endl;
740 cout << "i = " << i << endl;
741 cout << "j = " << i << endl;
742
743 // dotProduct of vector< double > ---------------------------------------------- //
744 cout << "dotProduct(x,y) = " << dotProduct(x,y) << endl;
745
746 // dotProduct of vector< int > ------------------------------------------------- //
747 cout << "dotProduct(i,j) = " << dotProduct(i,j) << endl;
748
749}
750
751// ================================================================================== //
752// OPERATOR "crossProduct" //
753// ================================================================================== //
754else if (selection.compare("crossProduct") == 0) {
755
756 // Scope variables -------------------------------------------------------------- //
757 dvector1D x(3, 0.0), y(3, 0.0);
758 ivector1D i(3, 1), j(3, 0);
759
760 // Initialize variables --------------------------------------------------------- //
761 x[0] = 1.05; x[1] = 0.0; x[2] = 0.0;
762 y[0] = 0.00; y[1] = 2.1; y[2] = 0.0;
763 i[0] = 0; i[1] = -2; i[2] = 0;
764 j[0] = 4; j[1] = 0; j[2] = 0;
765
766 // Output message --------------------------------------------------------------- //
767 cout << "x = " << x << endl;
768 cout << "y = " << x << endl;
769 cout << "i = " << i << endl;
770 cout << "j = " << i << endl;
771
772 // dotProduct of vector< double > ---------------------------------------------- //
773 cout << "crossProduct(x,y) = " << crossProduct(x,y) << endl;
774 cout << "crossProduct(x,x) = " << crossProduct(x,x) << endl;
775
776 // dotProduct of vector< int > ------------------------------------------------- //
777 cout << "crossProduct(i,j) = " << crossProduct(i,j) << endl;
778 cout << "crossProduct(i,i) = " << crossProduct(i,i) << endl;
779
780}
781
782// ================================================================================== //
783// OUTPUT MESSAGE //
784// ================================================================================== //
785{
786 cout << "DEMO: done!!" << endl;
787}
788
789return; };
790
791// ---------------------------------------------------------------------------------- //
792void arrayOperators_Ex(
793 void
794) {
795
796// ================================================================================== //
797// void arrayOperators_Ex( //
798// void) //
799// //
800// Examples of usage of operators for arrays //
801// ================================================================================== //
802// INPUT //
803// ================================================================================== //
804// - none //
805// ================================================================================== //
806// OUTPUT //
807// ================================================================================== //
808// - none //
809// ================================================================================== //
810
811// ================================================================================== //
812// VARIABLES DECLARATION //
813// ================================================================================== //
814
815// Local variables
816string selection;
817
818// Counters
819// none
820
821// ================================================================================== //
822// OUTPUT MESSAGE //
823// ================================================================================== //
824{
825 // Select sub case
826 cout << "DEMO: OPERATORS FOR STL ARRAYS" << endl;
827 cout << " Select operators: " << endl;
828 cout << " ""+"" operator" << endl;
829 cout << " ""-"" operator" << endl;
830 cout << " ""*"" operator" << endl;
831 cout << " ""/"" operator" << endl;
832 cin >> selection;
833}
834
835// ================================================================================== //
836// OPERATOR "+" //
837// ================================================================================== //
838if (selection.compare("+") == 0) {
839
840 // Scope variables
841 double a = 1, b = 2;
842 array<double, 2> x, y, z;
843 array<array<double, 2>, 2> X, Y, Z;
844
845 // Initialize vectors
846 x[0] = 1.0; x[1] = 2.0;
847 y = x;
848 X[0][0] = 1.0; X[0][1] = 2.0;
849 X[1][0] = 3.0; X[1][1] = 4.0;
850 Y = X;
851
852 // Variables
853 cout << "a = " << a << endl;
854 cout << "b = " << b << endl;
855 cout << "x = ";
856 cout << x << endl;
857 cout << "y = ";
858 cout << y << endl;
859 cout << "X = ";
860 cout << X << endl;
861 cout << "Y = ";
862 cout << Y << endl;
863
864 // Sum between vectors
865 cout << "sum between 1D arrays ----------------------------- " << endl;
866 cout << "x + y = ";
867 z = x+y;
868 cout << z << endl;
869 cout << "sum between 2D arrays ---------------------------- " << endl;
870 cout << "X + Y = ";
871 Z = X + Y;
872 cout << Z << endl;
873 cout << "sum between 1D/2D arrays ------------------------- " << endl;
874 cout << "x + Y = ";
875 Z = x + Y;
876 cout << Z << endl;
877 cout << "X + y = ";
878 Z = X + y;
879 cout << Z << endl;
880 cout << "sum between const and 1D arrays ------------------ " << endl;
881 cout << "a + x = ";
882 z = a + x;
883 cout << z << endl;
884 cout << "y + b = ";
885 z = y + b;
886 cout << z << endl;
887 cout << "sum between const and 2D arrays ------------------ " << endl;
888 cout << "a + X = ";
889 Z = a + X;
890 cout << Z << endl;
891 cout << "Y + b = ";
892 Z = Y + b;
893 cout << Z << endl;
894}
895
896// ================================================================================== //
897// OPERATOR "-" //
898// ================================================================================== //
899else if (selection.compare("-") == 0) {
900
901 // Scope variables
902 double a = 1, b = 2;
903 array<double, 2> x, y, z;
904 array<array<double, 2>, 2> X, Y, Z;
905
906 // Initialize vectors
907 x[0] = 1.0; x[1] = 2.0;
908 y = x;
909 X[0][0] = 1.0; X[0][1] = 2.0;
910 X[1][0] = 3.0; X[1][1] = 4.0;
911 Y = X;
912
913 // Variables
914 cout << "a = " << a << endl;
915 cout << "b = " << b << endl;
916 cout << "x = ";
917 cout << x << endl;
918 cout << "y = ";
919 cout << y << endl;
920 cout << "X = ";
921 cout << X << endl;
922 cout << "Y = ";
923 cout << Y << endl;
924
925 // Sum between vectors
926 cout << "diff between 1D arrays ---------------------------- " << endl;
927 cout << "x - y = ";
928 z = x - y;
929 cout << z << endl;
930 cout << "diff between 2D arrays ---------------------------- " << endl;
931 cout << "X - Y = ";
932 Z = X - Y;
933 cout << Z << endl;
934 cout << "diff between 1D/2D arrays ------------------------- " << endl;
935 cout << "x - Y = ";
936 Z = x - Y;
937 cout << Z << endl;
938 cout << "X - y = ";
939 Z = X - y;
940 cout << Z << endl;
941 cout << "diff between const and 1D arrays ------------------ " << endl;
942 cout << "a - x = ";
943 z = a - x;
944 cout << z << endl;
945 cout << "y - b = ";
946 z = y - b;
947 cout << z << endl;
948 cout << "diff between const and 2D arrays ------------------ " << endl;
949 cout << "a - X = ";
950 Z = a - X;
951 cout << Z << endl;
952 cout << "Y - b = ";
953 Z = Y - b;
954 cout << Z << endl;
955}
956
957// ================================================================================== //
958// OPERATOR "*" //
959// ================================================================================== //
960else if (selection.compare("*") == 0) {
961
962 // Scope variables
963 double a = 1, b = 2;
964 array<double, 2> x, y, z;
965 array<array<double, 2>, 2> X, Y, Z;
966
967 // Initialize vectors
968 x[0] = 1.0; x[1] = 2.0;
969 y = x;
970 X[0][0] = 1.0; X[0][1] = 2.0;
971 X[1][0] = 3.0; X[1][1] = 4.0;
972 Y = X;
973
974 // Variables
975 cout << "a = " << a << endl;
976 cout << "b = " << b << endl;
977 cout << "x = ";
978 cout << x << endl;
979 cout << "y = ";
980 cout << y << endl;
981 cout << "X = ";
982 cout << X << endl;
983 cout << "Y = ";
984 cout << Y << endl;
985
986 // Sum between vectors
987 cout << "prod between 1D arrays ---------------------------- " << endl;
988 cout << "x * y = ";
989 z = x * y;
990 cout << z << endl;
991 cout << "prod between 2D arrays ---------------------------- " << endl;
992 cout << "X * Y = ";
993 Z = X * Y;
994 cout << Z << endl;
995 cout << "prod between 1D/2D arrays ------------------------- " << endl;
996 cout << "x * Y = ";
997 Z = x * Y;
998 cout << Z << endl;
999 cout << "X * y = ";
1000 Z = X * y;
1001 cout << Z << endl;
1002 cout << "prod between const and 1D arrays ------------------ " << endl;
1003 cout << "a * x = ";
1004 z = a * x;
1005 cout << z << endl;
1006 cout << "y * b = ";
1007 z = y * b;
1008 cout << z << endl;
1009 cout << "prod between const and 2D arrays ------------------ " << endl;
1010 cout << "a * X = ";
1011 Z = a * X;
1012 cout << Z << endl;
1013 cout << "Y * b = ";
1014 Z = Y * b;
1015 cout << Z << endl;
1016}
1017
1018// ================================================================================== //
1019// OPERATOR "/" //
1020// ================================================================================== //
1021else if (selection.compare("/") == 0) {
1022
1023 // Scope variables
1024 double a = 1, b = 2;
1025 array<double, 2> x, y, z;
1026 array<array<double, 2>, 2> X, Y, Z;
1027
1028 // Initialize vectors
1029 x[0] = 1.0; x[1] = 2.0;
1030 y = x;
1031 X[0][0] = 1.0; X[0][1] = 2.0;
1032 X[1][0] = 3.0; X[1][1] = 4.0;
1033 Y = X;
1034
1035 // Variables
1036 cout << "a = " << a << endl;
1037 cout << "b = " << b << endl;
1038 cout << "x = " << endl;
1039 cout << x << endl;
1040 cout << "y = ";
1041 cout << y << endl;
1042 cout << "X = ";
1043 cout << X << endl;
1044 cout << "Y = ";
1045 cout << Y << endl;
1046
1047 // Sum between vectors
1048 cout << "div between 1D arrays ---------------------------- " << endl;
1049 cout << "x / y = ";
1050 z = x / y;
1051 cout << z << endl;
1052 cout << "div between 2D arrays ---------------------------- " << endl;
1053 cout << "X / Y = ";
1054 Z = X / Y;
1055 cout << Z << endl;
1056 cout << "div between 1D/2D arrays ------------------------- " << endl;
1057 cout << "x / Y = ";
1058 Z = x / Y;
1059 cout << Z << endl;
1060 cout << "X / y = ";
1061 Z = X / y;
1062 cout << Z << endl;
1063 cout << "div between const and 1D arrays ------------------ " << endl;
1064 cout << "a / x = ";
1065 z = a / x;
1066 cout << z << endl;
1067 cout << "y / b = ";
1068 z = y / b;
1069 cout << z << endl;
1070 cout << "div between const and 2D arrays ------------------ " << endl;
1071 cout << "a / X = ";
1072 Z = a / X;
1073 cout << Z << endl;
1074 cout << "Y / b = ";
1075 Z = Y / b;
1076 cout << Z << endl;
1077}
1078
1079// ================================================================================== //
1080// OPERATOR "not available" //
1081// ================================================================================== //
1082else {
1083 cout << " Operator not available!" << endl;
1084}
1085
1086// ================================================================================== //
1087// OUTPUT MESSAGE //
1088// ================================================================================== //
1089{
1090 cout << "DEMO: done!!" << endl;
1091}
1092
1093return; };
1094
1095// ---------------------------------------------------------------------------------- //
1096void arrayMathFunct_Ex(
1097 void
1098) {
1099
1100// ================================================================================== //
1101// void arrayMathFunct_Ex( //
1102// void) //
1103// //
1104// Examples of usage of operators for C++ v10.0 arrays. //
1105// ================================================================================== //
1106// INPUT //
1107// ================================================================================== //
1108// - none //
1109// ================================================================================== //
1110// OUTPUT //
1111// ================================================================================== //
1112// - none //
1113// ================================================================================== //
1114
1115// ================================================================================== //
1116// VARIABLES DECLARATION //
1117// ================================================================================== //
1118
1119// Local variables
1120string selection;
1121
1122// Counters
1123// none
1124
1125// ================================================================================== //
1126// OUTPUT MESSAGE //
1127// ================================================================================== //
1128{
1129 // Select sub case
1130 cout << "DEMO: MATH FUNCTIONS FOR STL ARRAYS" << endl;
1131 cout << " Select operators: " << endl;
1132 cout << " 'min' operator" << endl;
1133 cout << " 'max' operator" << endl;
1134 cout << " 'minval' operator" << endl;
1135 cout << " 'maxval' operator" << endl;
1136 cout << " 'sum' operator" << endl;
1137 cout << " 'abs' operator" << endl;
1138 cout << " 'pow' operator" << endl;
1139 cout << " 'norm' operator" << endl;
1140 cout << " 'dotProduct' operator" << endl;
1141 cout << " 'crossProduct' operator" << endl;
1142 cin >> selection;
1143
1144}
1145
1146// ================================================================================== //
1147// OPERATOR "min" //
1148// ================================================================================== //
1149if (selection.compare("min") == 0) {
1150
1151 // Scope variables -------------------------------------------------------------- //
1152 double a = 1.5;
1153 array<double, 3> x, t;
1154 array<array<double, 3>, 3> y, z;
1155
1156 // Initialize scope variables --------------------------------------------------- //
1157 x[0] = 3.0; x[1] = 3.0; x[2] = 3.0;
1158 t[0] = 1.0; t[1] = 4.0; t[2] = 2.0;
1159 y[0][0] = 0.0; y[0][1] = 1.0; y[0][2] = 2.0;
1160 y[1][0] = 3.0; y[1][1] = 4.0; y[1][2] = 5.0;
1161 y[2][0] = 6.0; y[2][1] = 7.0; y[2][2] = 8.0;
1162 z[0][0] = 2.0; z[0][1] = 2.0; z[0][2] = 2.0;
1163 z[1][0] = 2.0; z[1][1] = 2.0; z[1][2] = 2.0;
1164 z[2][0] = 2.0; z[2][1] = 2.0; z[2][2] = 2.0;
1165 cout << "a = " << a << endl;
1166 cout << "x = " << x << endl;
1167 cout << "t = " << t << endl;
1168 cout << "y = " << y << endl;
1169 cout << "z = " << z << endl;
1170
1171 // min between scalar and 1D array ---------------------------------------------- //
1172 cout << "min(t, a) = " << min(t,a) << endl;
1173
1174 // min between 1D arrays -------------------------------------------------------- //
1175 cout << "min(x, t) = " << min(x, t) << endl;
1176
1177 // min between scalar and 2D array ---------------------------------------------- //
1178 cout << "min(y, a) = " << min(y, a) << endl;
1179
1180 // min between 1D array and 2D array -------------------------------------------- //
1181 cout << "min(y, x) = " << min(y, x) << endl;
1182
1183 // min between 2D arrays -------------------------------------------------------- //
1184 cout << "min(y, z) = " << min(y, z) << endl;
1185
1186}
1187
1188// ================================================================================== //
1189// OPERATOR "minval" //
1190// ================================================================================== //
1191if (selection.compare("minval") == 0) {
1192
1193 // Scope variables -------------------------------------------------------------- //
1194 int nn = 0;
1195 double a = 3.0, mm;
1196 array<double, 3> x;
1197 array<int, 3> i;
1198 array<array<int, 3>, 3> y;
1199
1200 // Initialize variables --------------------------------------------------------- //
1201 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
1202 i[0] = 4; i[1] = 1; i[2] = 4;
1203 y[0] = 2*i;
1204 y[1] = i;
1205 y[2] = -3*i;
1206
1207 // Output message --------------------------------------------------------------- //
1208 cout << "a = " << a << endl;
1209 cout << "x = " << x << endl;
1210 cout << "y = " << y << endl;
1211
1212 // Minval of double ------------------------------------------------------------- //
1213 minval(a,mm);
1214 cout << "minval(a) = " << mm << endl;
1215
1216 // Minval of 1D vector ---------------------------------------------------------- //
1217 minval(x,mm);
1218 cout << "minval(x) = " << mm << endl;
1219
1220 // Minval of 2D vector ---------------------------------------------------------- //
1221 minval(y,nn);
1222 cout << "minval(y) = " << nn << endl;
1223
1224}
1225
1226// ================================================================================== //
1227// OPERATOR "max" //
1228// ================================================================================== //
1229else if (selection.compare("max") == 0) {
1230
1231 // Scope variables -------------------------------------------------------------- //
1232 double a = 1.5;
1233 array<double, 3> x, t;
1234 array<array<double, 3>, 3> y, z;
1235
1236 // Initialize scope variables --------------------------------------------------- //
1237 x[0] = 3.0; x[1] = 3.0; x[2] = 3.0;
1238 t[0] = 1.0; t[1] = 4.0; t[2] = 2.0;
1239 y[0][0] = 0.0; y[0][1] = 1.0; y[0][2] = 2.0;
1240 y[1][0] = 3.0; y[1][1] = 4.0; y[1][2] = 5.0;
1241 y[2][0] = 6.0; y[2][1] = 7.0; y[2][2] = 8.0;
1242 z[0][0] = 2.0; z[0][1] = 2.0; z[0][2] = 2.0;
1243 z[1][0] = 2.0; z[1][1] = 2.0; z[1][2] = 2.0;
1244 z[2][0] = 2.0; z[2][1] = 2.0; z[2][2] = 2.0;
1245 cout << "a = " << a << endl;
1246 cout << "x = " << x << endl;
1247 cout << "t = " << t << endl;
1248 cout << "y = " << y << endl;
1249 cout << "z = " << z << endl;
1250
1251 // min between scalar and 1D array ---------------------------------------------- //
1252 cout << "max(t, a) = " << max(t,a) << endl;
1253
1254 // min between 1D arrays -------------------------------------------------------- //
1255 cout << "max(x, t) = " << max(x, t) << endl;
1256
1257 // min between scalar and 2D array ---------------------------------------------- //
1258 cout << "max(y, a) = " << max(y, a) << endl;
1259
1260 // min between 1D array and 2D array -------------------------------------------- //
1261 cout << "max(y, x) = " << max(y, x) << endl;
1262
1263 // min between 2D arrays -------------------------------------------------------- //
1264 cout << "max(y, z) = " << max(y, z) << endl;
1265
1266}
1267
1268// ================================================================================== //
1269// OPERATOR "maxval" //
1270// ================================================================================== //
1271if (selection.compare("maxval") == 0) {
1272
1273 // Scope variables -------------------------------------------------------------- //
1274 int nn = 0;
1275 double a = 3.0, mm;
1276 array<double, 3> x;
1277 array<int, 3> i;
1278 array<array<int, 3>, 3> y;
1279
1280 // Initialize variables --------------------------------------------------------- //
1281 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
1282 i[0] = 1; i[1] = 2; i[2] = 4;
1283 y[0] = i;
1284 y[1] = 2*i;
1285 y[2] = -3*i;
1286
1287 // Output message --------------------------------------------------------------- //
1288 cout << "a = " << a << endl;
1289 cout << "x = " << x << endl;
1290 cout << "y = " << y << endl;
1291
1292 // Minval of double ------------------------------------------------------------- //
1293 maxval(a,mm);
1294 cout << "maxval(a) = " << mm << endl;
1295
1296 // Minval of 1D vector ---------------------------------------------------------- //
1297 maxval(x,mm);
1298 cout << "maxval(x) = " << mm << endl;
1299
1300 // Minval of 2D vector ---------------------------------------------------------- //
1301 maxval(y,nn);
1302 cout << "maxval(y) = " << nn << endl;
1303
1304}
1305
1306// ================================================================================== //
1307// OPERATOR "sum" //
1308// ================================================================================== //
1309else if (selection.compare("sum") == 0) {
1310
1311 // Scope variables -------------------------------------------------------------- //
1312 int nn = -5;
1313 double a = 3.0, mm;
1314 array<double, 3> x;
1315 array<int, 3> i;
1316 array<array<int, 3>, 3> y;
1317
1318 // Initialize variables --------------------------------------------------------- //
1319 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
1320 i[0] = 1; i[1] = 2; i[2] = 4;
1321 y[0] = i;
1322 y[1] = 2*i;
1323 y[2] = 3*i;
1324
1325 // Output message --------------------------------------------------------------- //
1326 cout << "a = " << a << endl;
1327 cout << "x = " << x << endl;
1328 cout << "y = " << y << endl;
1329
1330 // Minval of double ------------------------------------------------------------- //
1331 sum(a,mm);
1332 cout << "sum(a) = " << mm << endl;
1333
1334 // Minval of 1D vector ---------------------------------------------------------- //
1335 sum(x,mm);
1336 cout << "sum(x) = " << mm << endl;
1337
1338 // Minval of 2D vector ---------------------------------------------------------- //
1339 sum(y,nn);
1340 cout << "sum(y) = " << nn << endl;
1341
1342}
1343
1344// ================================================================================== //
1345// OPERATOR "abs" //
1346// ================================================================================== //
1347else if (selection.compare("abs") == 0) {
1348
1349 // Scope variables -------------------------------------------------------------- //
1350 double a = 3.0;
1351 array<double, 3> x;
1352 array<int, 3> i;
1353 array<array<int, 3>, 3> y;
1354
1355 // Initialize variables --------------------------------------------------------- //
1356 x[0] = 1.05; x[1] = -1.1; x[2] = 3.0;
1357 i[0] = 1; i[1] = 2; i[2] = 4;
1358 y[0] = i;
1359 y[1] = 2*i;
1360 y[2] = -1*i;
1361
1362 // Output message --------------------------------------------------------------- //
1363 cout << "a = " << a << endl;
1364 cout << "x = " << x << endl;
1365 cout << "y = " << y << endl;
1366
1367 // Minval of double ------------------------------------------------------------- //
1368 cout << "abs(a) = " << abs(a) << endl;
1369
1370 // Minval of 1D vector ---------------------------------------------------------- //
1371 cout << "abs(x) = " << abs(x) << endl;
1372
1373 // Minval of 2D vector ---------------------------------------------------------- //
1374 cout << "abs(y) = " << abs(y) << endl;
1375
1376}
1377
1378// ================================================================================== //
1379// OPERATOR "pow" //
1380// ================================================================================== //
1381else if (selection.compare("pow") == 0) {
1382
1383 // Scope variables -------------------------------------------------------------- //
1384 double a = 3.0;
1385 array<double, 3> x;
1386 array<int, 3> i;
1387 array<array<int, 3>, 3> y;
1388
1389 // Initialize variables --------------------------------------------------------- //
1390 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
1391 i[0] = 1; i[1] = 2; i[2] = 4;
1392 y[0] = i;
1393 y[1] = 2*i;
1394 y[2] = 3*i;
1395
1396 // Output message --------------------------------------------------------------- //
1397 cout << "a = " << a << endl;
1398 cout << "x = " << x << endl;
1399 cout << "y = " << y << endl;
1400
1401 // Minval of double ------------------------------------------------------------- //
1402 cout << "pow(a,2) = " << pow(a,2.0) << endl;
1403
1404 // Minval of 1D vector ---------------------------------------------------------- //
1405 cout << "pow(x,2) = " << pow(x,2.0) << endl;
1406
1407 // Minval of 2D vector ---------------------------------------------------------- //
1408 cout << "pow(y,2) = " << pow(y,2.0) << endl;
1409
1410}
1411
1412// ================================================================================== //
1413// OPERATOR "norm" //
1414// ================================================================================== //
1415else if (selection.compare("norm") == 0) {
1416
1417 // Scope variables -------------------------------------------------------------- //
1418 array<double, 3> x;
1419 array<int, 3> i;
1420
1421 // Initialize variables --------------------------------------------------------- //
1422 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
1423 i[0] = 1; i[1] = -2; i[2] = -4;
1424
1425 // Output message --------------------------------------------------------------- //
1426 cout << "x = " << x << endl;
1427 cout << "i = " << i << endl;
1428
1429 // norm of vector< double > ----------------------------------------------------- //
1430 cout << "norm(x,2) = " << norm(x,2) << endl;
1431 cout << "norm(x,3) = " << norm(x,3) << endl;
1432 cout << "normInf(x) = " << normInf(x) << endl;
1433
1434 // norm of vector< int > -------------------------------------------------------- //
1435 cout << "norm(i,2) = " << norm(i,2) << endl;
1436 cout << "norm(i,3) = " << norm(i,3) << endl;
1437 cout << "normInf(i) = " << normInf(i) << endl;
1438
1439}
1440
1441// ================================================================================== //
1442// OPERATOR "dotProduct" //
1443// ================================================================================== //
1444else if (selection.compare("dotProduct") == 0) {
1445
1446 // Scope variables -------------------------------------------------------------- //
1447 array<double, 3> x, y;
1448 array<int, 3> i, j;
1449
1450 // Initialize variables --------------------------------------------------------- //
1451 x[0] = 1.05; x[1] = 1.1; x[2] = 3.0;
1452 y = x;
1453 i[0] = 1; i[1] = -2; i[2] = 4;
1454 j = i;
1455
1456 // Output message --------------------------------------------------------------- //
1457 cout << "x = " << x << endl;
1458 cout << "y = " << x << endl;
1459 cout << "i = " << i << endl;
1460 cout << "j = " << i << endl;
1461
1462 // dotProduct of vector< double > ---------------------------------------------- //
1463 cout << "dotProduct(x,y) = " << dotProduct(x,y) << endl;
1464
1465 // dotProduct of vector< int > ------------------------------------------------- //
1466 cout << "dotProduct(i,j) = " << dotProduct(i,j) << endl;
1467
1468}
1469
1470// ================================================================================== //
1471// OPERATOR "crossProduct" //
1472// ================================================================================== //
1473else if (selection.compare("crossProduct") == 0) {
1474
1475 // Scope variables -------------------------------------------------------------- //
1476 dvector1D x(3, 0.0), y(3, 0.0);
1477 ivector1D i(3, 1), j(3, 0);
1478
1479 // Initialize variables --------------------------------------------------------- //
1480 x[0] = 1.05; x[1] = 0.0; x[2] = 0.0;
1481 y[0] = 0.00; y[1] = 2.1; y[2] = 0.0;
1482 i[0] = 0; i[1] = -2; i[2] = 0;
1483 j[0] = 4; j[1] = 0; j[2] = 0;
1484
1485 // Output message --------------------------------------------------------------- //
1486 cout << "x = " << x << endl;
1487 cout << "y = " << x << endl;
1488 cout << "i = " << i << endl;
1489 cout << "j = " << i << endl;
1490
1491 // dotProduct of vector< double > ---------------------------------------------- //
1492 cout << "crossProduct(x,y) = " << crossProduct(x,y) << endl;
1493 cout << "crossProduct(x,x) = " << crossProduct(x,x) << endl;
1494
1495 // dotProduct of vector< int > ------------------------------------------------- //
1496 cout << "crossProduct(i,j) = " << crossProduct(i,j) << endl;
1497 cout << "crossProduct(i,i) = " << crossProduct(i,i) << endl;
1498
1499}
1500
1501// ================================================================================== //
1502// OUTPUT MESSAGE //
1503// ================================================================================== //
1504{
1505 cout << "DEMO: done!!" << endl;
1506}
1507
1508return; };
1509
1510// ================================================================================== //
1511// MAIN //
1512// ================================================================================== //
1513int main(void) {
1514
1515// ================================================================================== //
1516// VARIABLES DECLARATION //
1517// ================================================================================== //
1518int selection;
1519
1520// ================================================================================== //
1521// SELECT CASE //
1522// ================================================================================== //
1523cout << "SELECT DEMO: " << endl;
1524cout << "0. Basic Operators for STL vectors" << endl;
1525cout << "1. Math Operators for STL vectors" << endl;
1526cout << "2. Basic Operators for STL array" << endl;
1527cout << "3. Math Operators for STL array" << endl;
1528cin >> selection;
1529try {
1530 switch (selection) {
1531 case 0: { vectorOperators_Ex(); break; }
1532 case 1: { vectorMathFunct_Ex(); break; }
1533 case 2: { arrayOperators_Ex(); break; }
1534 case 3: { arrayMathFunct_Ex(); break; }
1535 }
1536} catch (const std::exception &exception) {
1537 std::cout << exception.what();
1538 exit(1);
1539}
1540
1541return(0); };
std::array< T, d > abs(const std::array< T, d > &x)
void sum(const std::array< T, d > &x, T1 &s)
double norm(const std::array< T, d > &x, int p)
std::array< T, 3 > crossProduct(const std::array< T, 3 > &x, const std::array< T, 3 > &y)
void maxval(const std::array< T, d > &x, T1 &max_value)
void minval(const std::array< T, d > &x, T1 &min_value)
T dotProduct(const std::array< T, d > &x, const std::array< T, d > &y)
std::array< T, d > max(const std::array< T, d > &x, const std::array< T, d > &y)
double normInf(const std::array< T, d > &x)
std::array< T, d > pow(std::array< T, d > &x, double p)
std::array< T, d > min(const std::array< T, d > &x, const std::array< T, d > &y)
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
--- layout: doxygen_footer ---