[4057] | 1 | #include "sopnamsp.h"
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include "math.h"
|
---|
| 6 | #include <iostream>
|
---|
| 7 | #include <string>
|
---|
| 8 | #include <vector>
|
---|
| 9 |
|
---|
| 10 | #include <typeinfo>
|
---|
| 11 |
|
---|
| 12 | #include "sophyainit.h"
|
---|
| 13 | #include "randr48.h"
|
---|
| 14 | #include "timing.h"
|
---|
| 15 | #include "ctimer.h"
|
---|
| 16 | #include "pqnumber.h"
|
---|
| 17 | #include "sunitpcst.h"
|
---|
| 18 |
|
---|
| 19 | /* Programme test des classes PrimeNumbers, QNumber ... */
|
---|
| 20 | /* SOPHYA - R. Ansari (LAL) - Janvier 2012 */
|
---|
| 21 |
|
---|
| 22 | int main(int narg, char* arg[])
|
---|
| 23 | {
|
---|
| 24 | cout << " ---- Programme tpqunits.cc - Test classe PrimeNumbers, QNumber --- " << endl;
|
---|
| 25 | int rc = 0;
|
---|
| 26 | try {
|
---|
| 27 | SophyaInit();
|
---|
| 28 | /*
|
---|
| 29 | PPRegister(ObjFileIO<Units>);
|
---|
| 30 | DObjRegister(ObjFileIO<Units>, Units);
|
---|
| 31 | PPRegister(ObjFileIO<PhysQty>);
|
---|
| 32 | DObjRegister(ObjFileIO<PhysQty>, PhysQty);
|
---|
| 33 | */
|
---|
| 34 | InitTim();
|
---|
| 35 | cout << " Sizeof(PrimeNumbers=) " << sizeof(PrimeNumbers) << endl;
|
---|
| 36 | cout << " Sizeof(QNumber=) " << sizeof(QNumber) << endl;
|
---|
| 37 |
|
---|
| 38 | cout << " ------------------------------------------------ " << endl;
|
---|
| 39 | cout << " ------- Test de la classe PrimeNumbers -------- " << endl;
|
---|
| 40 | // Create a PrimeNumbers object
|
---|
| 41 | PrimeNumbers primes;
|
---|
| 42 | int NBPRTPRIMES=50;
|
---|
| 43 | // Print the first NBPRTPRIMES primes ...
|
---|
| 44 | cout << "Test_PrimeNumbers: First " << NBPRTPRIMES << " prime numbers : " << endl;
|
---|
| 45 | for(size_t k=0; k<NBPRTPRIMES; k++) {
|
---|
| 46 | cout << primes(k) << " , ";
|
---|
| 47 | if (k%10==0) cout << endl;
|
---|
| 48 | }
|
---|
| 49 | cout << endl;
|
---|
| 50 | PrtTim("End first 50 primes");
|
---|
| 51 | cout << " primes[1000000]=" << primes[1000000] << endl;
|
---|
| 52 | PrtTim("End primes[1000000]");
|
---|
| 53 | cout << endl;
|
---|
| 54 | // Print all prime numbers less than some max values
|
---|
| 55 | uint_8 pmax = 1023;
|
---|
| 56 | cout << "Test_PrimeNumbers: prime numbers less than " << pmax << endl;
|
---|
| 57 | uint_8 p = primes.Next();
|
---|
| 58 | int cnt=0;
|
---|
| 59 | while (p<pmax) {
|
---|
| 60 | cout << p << " , "; cnt++;
|
---|
| 61 | if (cnt%10==0) cout << endl;
|
---|
| 62 | p = primes.Next();
|
---|
| 63 | }
|
---|
| 64 | cout << endl;
|
---|
| 65 | uint_8 nombre,nombres[6]={247890,1542297,8255846,2946868,1570889,22702166};
|
---|
| 66 | const char* decomps[6]={"2 x 3 x 5 x 8263","3 x 41 x 12539","2 x 17 x 242819","2 x 2 x 736717","1570889","2 x 11351083"};
|
---|
| 67 | for(int i=0; i<6; i++) {
|
---|
| 68 | nombre=nombres[i];
|
---|
| 69 | PrimeNumbers::PrimeFactors(nombre, true);
|
---|
| 70 | cout << " ...MathematicaCheck: OK if == " << decomps[i] << endl;
|
---|
| 71 | }
|
---|
| 72 | cout << endl;
|
---|
| 73 | DR48RandGen rnd;
|
---|
| 74 | rnd.AutoInit();
|
---|
| 75 | for(int i=0; i<5; i++) {
|
---|
| 76 | nombre=rnd.Flat01()*30000000.;
|
---|
| 77 | PrimeNumbers::PrimeFactors(nombre, true);
|
---|
| 78 | }
|
---|
| 79 | cout << endl;
|
---|
| 80 | PrtTim("End PrimeFactors");
|
---|
| 81 |
|
---|
| 82 | cout << endl;
|
---|
| 83 | cout << " ------------------------------------------------ " << endl;
|
---|
| 84 | cout << " -------- Test de la classe QNumber -------- " << endl;
|
---|
| 85 | // Create rational numbers from integers
|
---|
| 86 | QNumber q1(3,4), q2(5,6), q3(6,8);
|
---|
| 87 | // Operations and comparsion
|
---|
| 88 | QNumber q4 = q1+q2;
|
---|
| 89 | cout << "Test_QNumber q4=q1+q2: " << q1 << " + " << q2 << " = " << q4
|
---|
| 90 | << " q4.Simplify()=" << q4.Simplify() << endl;
|
---|
| 91 | QNumber qa(q1), qb(q2);
|
---|
| 92 | cout << "Test_QNumber if(qa==qb) : -> " << qa << " ?== " << qb;
|
---|
| 93 | if (qa == qb) cout << " -> true " << endl;
|
---|
| 94 | else cout << " -> false " << endl;
|
---|
| 95 | qb = q3;
|
---|
| 96 | cout << "Test_QNumber if(qa==qb) : -> " << qa << " ?== " << qb;
|
---|
| 97 | if (qa == qb) cout << " -> true " << endl;
|
---|
| 98 | else cout << " -> false " << endl;
|
---|
| 99 | cout << "Test_QNumber.Simplify: [ 3*7*17*5,(-5)*7*11*2 ] : " << endl;
|
---|
| 100 | QNumber qq(3*7*17*5,(-5)*7*11*2);
|
---|
| 101 | cout << " qq= " << qq << " qq.Simplify() = " << qq.Simplify() << endl;
|
---|
| 102 |
|
---|
| 103 | cout << endl;
|
---|
| 104 | cout << " ------------------------------------------------ " << endl;
|
---|
| 105 | cout << " ---- Test de la classe Units ---- " << endl;
|
---|
| 106 | cout << " units::meter() : " << Units::meter() << " ( ";
|
---|
| 107 | Units::meter().Print(cout,1) << " ) " << endl;
|
---|
| 108 | Units::meter().Print() << endl;
|
---|
| 109 |
|
---|
| 110 | cout << " units::kilogram() : " << Units::kilogram() << endl;
|
---|
| 111 | cout << " units::second() : " << Units::second() << endl;
|
---|
| 112 |
|
---|
| 113 | Units mos = Units::meter()/Units::second() ;
|
---|
| 114 | cout << " units::m/s : " << mos << " -> "; mos.Print() ;
|
---|
| 115 |
|
---|
| 116 | cout << " units::watt() : " << Units::watt() << endl;
|
---|
| 117 | Units::watt().Print() << endl;
|
---|
| 118 |
|
---|
| 119 | cout << " units::watt().mega() : " << Units::watt().mega() << endl;
|
---|
| 120 | Units::watt().mega().Print() << endl;
|
---|
| 121 |
|
---|
| 122 | cout << endl;
|
---|
| 123 | cout << " ------------------------------------------------ " << endl;
|
---|
| 124 | cout << " ---- Test de la classe PhysQty ---- " << endl;
|
---|
| 125 | cout << " c (speed of light): " << PhysQty::c() << endl;
|
---|
| 126 | cout << " k_Boltzmann: " << PhysQty::k() << endl;
|
---|
| 127 | cout << " G_Newton: " << PhysQty::G() << endl;
|
---|
| 128 | cout << " h_Planck: " << PhysQty::h() << endl;
|
---|
| 129 | cout << " R: " << PhysQty::R() << endl;
|
---|
| 130 | cout << " N_A: " << PhysQty::N_A() << endl;
|
---|
| 131 | cout << " m_e: " << PhysQty::electron_mass() << endl;
|
---|
| 132 | cout << " m_p: " << PhysQty::proton_mass() << endl;
|
---|
| 133 |
|
---|
| 134 | {
|
---|
| 135 | cout << " --->> Writing watt, c, k_Boltzmann, G_Newton to tpqunits.ppf ... " << endl;
|
---|
| 136 | POutPersist po("tpqunits.ppf");
|
---|
| 137 | Units uw = Units::watt();
|
---|
| 138 | PhysQty c = PhysQty::c();
|
---|
| 139 | PhysQty k = PhysQty::k();
|
---|
| 140 | PhysQty G = PhysQty::G();
|
---|
| 141 | po << uw << c << k << G;
|
---|
| 142 | }
|
---|
| 143 | {
|
---|
| 144 | cout << " --->> Reading unit u1, physical quantities ( pq1 pq2 pq3 ) from tpqunits.ppf ... " << endl;
|
---|
| 145 | PInPersist pi("tpqunits.ppf");
|
---|
| 146 | Units u1;
|
---|
| 147 | PhysQty pq1,pq2,pq3;
|
---|
| 148 | pi >> u1 >> pq1 >> pq2 >> pq3;
|
---|
| 149 | u1.Print(2);
|
---|
| 150 | pq1.Print(1) << endl;
|
---|
| 151 | pq2.Print(1) << endl;
|
---|
| 152 | pq3.Print(1) << endl;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | }
|
---|
| 156 | catch (PThrowable & exc) {
|
---|
| 157 | cerr << " tpqunits.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name()
|
---|
| 158 | << " - Msg= " << exc.Msg() << endl;
|
---|
| 159 | rc = 99;
|
---|
| 160 | }
|
---|
| 161 | catch (std::exception & e) {
|
---|
| 162 | cerr << " tpqunits.cc: Catched std::xception "
|
---|
| 163 | << " - what()= " << e.what() << endl;
|
---|
| 164 | rc = 98;
|
---|
| 165 | }
|
---|
| 166 | catch (...) {
|
---|
| 167 | cerr << " tpqunits.cc: some other exception (...) was caught ! " << endl;
|
---|
| 168 | rc = 97;
|
---|
| 169 | }
|
---|
| 170 | PrtTim("End tpqunits " );
|
---|
| 171 | cout << " ---- Programme tpqunits.cc- FIN (Rc=" << rc << ") --- " << endl;
|
---|
| 172 | return rc;
|
---|
| 173 | }
|
---|