File: tenseur.C

package info (click to toggle)
lorene 0.0.0~cvs20161116%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,472 kB
  • sloc: cpp: 212,946; fortran: 21,645; makefile: 1,750; sh: 4
file content (1585 lines) | stat: -rw-r--r-- 39,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
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
509
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
552
553
554
555
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
/*
 *  Methods of class Tenseur
 *
 *   (see file tenseur.h for documentation)
 *
 */

/*
 *   Copyright (c) 1999-2001 Philippe Grandclement
 *   Copyright (c) 2000-2001 Eric Gourgoulhon
 *   Copyright (c) 2002 Jerome Novak
 *
 *   This file is part of LORENE.
 *
 *   LORENE is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   LORENE is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with LORENE; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


char tenseur_C[] = "$Header: /cvsroot/Lorene/C++/Source/Tenseur/tenseur.C,v 1.15 2014/10/13 08:53:41 j_novak Exp $" ;

/*
 * $Id: tenseur.C,v 1.15 2014/10/13 08:53:41 j_novak Exp $
 * $Log: tenseur.C,v $
 * Revision 1.15  2014/10/13 08:53:41  j_novak
 * Lorene classes and functions now belong to the namespace Lorene.
 *
 * Revision 1.14  2014/10/06 15:13:18  j_novak
 * Modified #include directives to use c++ syntax.
 *
 * Revision 1.13  2003/03/03 19:37:31  f_limousin
 * Suppression of many assert(verif()).
 *
 * Revision 1.12  2002/10/16 14:37:14  j_novak
 * Reorganization of #include instructions of standard C++, in order to
 * use experimental version 3 of gcc.
 *
 * Revision 1.11  2002/09/06 14:49:25  j_novak
 * Added method lie_derive for Tenseur and Tenseur_sym.
 * Corrected various errors for derive_cov and arithmetic.
 *
 * Revision 1.10  2002/08/30 13:21:38  j_novak
 * Corrected error in constructor
 *
 * Revision 1.9  2002/08/14 13:46:15  j_novak
 * Derived quantities of a Tenseur can now depend on several Metrique's
 *
 * Revision 1.8  2002/08/13 08:02:45  j_novak
 * Handling of spherical vector/tensor components added in the classes
 * Mg3d and Tenseur. Minor corrections for the class Metconf.
 *
 * Revision 1.7  2002/08/08 15:10:45  j_novak
 * The flag "plat" has been added to the class Metrique to show flat metrics.
 *
 * Revision 1.6  2002/08/07 16:14:11  j_novak
 * class Tenseur can now also handle tensor densities, this should be transparent to older codes
 *
 * Revision 1.5  2002/08/02 15:07:41  j_novak
 * Member function determinant has been added to the class Metrique.
 * A better handling of spectral bases is now implemented for the class Tenseur.
 *
 * Revision 1.4  2002/05/07 07:36:03  e_gourgoulhon
 * Compatibilty with xlC compiler on IBM SP2:
 *    suppressed the parentheses around argument of instruction new:
 * 	e.g.   t = new (Tbl *[nzone])  -->   t = new Tbl*[nzone]
 *
 * Revision 1.3  2002/05/02 15:16:22  j_novak
 * Added functions for more general bi-fluid EOS
 *
 * Revision 1.2  2001/12/04 21:27:54  e_gourgoulhon
 *
 * All writing/reading to a binary file are now performed according to
 * the big endian convention, whatever the system is big endian or
 * small endian, thanks to the functions fwrite_be and fread_be
 *
 * Revision 1.1.1.1  2001/11/20 15:19:30  e_gourgoulhon
 * LORENE
 *
 * Revision 2.21  2001/10/10  13:54:40  eric
 * Modif Joachim: pow(3, *) --> pow(3., *)
 *
 * Revision 2.20  2000/12/20  09:50:08  eric
 * Correction erreur dans operator<< : la sortie doit etre flux et non cout !
 *
 * Revision 2.19  2000/10/12  13:11:23  eric
 * Methode set_std_base(): traitement du cas etat = ETATZERO (return).
 *
 * Revision 2.18  2000/09/13  12:11:40  eric
 * Ajout de la fonction allocate_all().
 *
 * Revision 2.17  2000/05/22  14:40:09  phil
 * ajout de inc_dzpuis et dec_dzpuis
 *
 * Revision 2.16  2000/03/22  09:18:57  eric
 * Traitement du cas ETATZERO dans dec2_dzpuis, inc2_dzpuis et mult_r_zec.
 *
 * Revision 2.15  2000/02/12  11:35:58  eric
 * Modif de la fonction set_std_base : appel de Valeur::set_base plutot
 * que l'affectation directe du membre Valeur::base.
 *
 * Revision 2.14  2000/02/10  18:30:47  eric
 * La fonction set_triad ne fait plus que l'affectation du membre triad.
 *
 * Revision 2.13  2000/02/10  16:11:07  eric
 * Ajout de la fonction change_triad.
 *
 * Revision 2.12  2000/02/09  19:32:39  eric
 * MODIF IMPORTANTE: la triade de decomposition est desormais passee en
 * argument des constructeurs.
 *
 * Revision 2.11  2000/01/24  13:02:39  eric
 * Traitement du cas triad = 0x0 dans la sauvegarde/lecture fichier
 * Constructeur par lecture de fichier: met_depend est desormais initialise
 *  a 0x0.
 *
 * Revision 2.10  2000/01/20  16:02:57  eric
 * Ajout des operator=(double ) et operator=(int ).
 *
 * Revision 2.9  2000/01/20  15:34:39  phil
 * changement traid dans fait_gradient()
 *
 * Revision 2.8  2000/01/14  14:07:26  eric
 * Ajout de la fonction annule.
 *
 * Revision 2.7  2000/01/13  14:10:53  eric
 * Ajout du constructeur par copie d'un Cmp (pour un scalaire)
 * ainsi que l'affectation a un Cmp.
 *
 * Revision 2.6  2000/01/13  13:46:38  eric
 * Ajout du membre p_gradient_spher et des fonctions fait_gradient_spher(),
 *  gradient_spher() pour le calcul du gradient d'un scalaire en
 *  coordonnees spheriques sur la triade spherique associee.
 *
 * Revision 2.5  2000/01/12  13:19:04  eric
 * Les operator::(...) renvoient desormais une reference const sur le c[...]
 * correspondant et non plus un Cmp copie de c[...].
 * (ceci grace a la nouvelle fonction Map::cmp_zero()).
 *
 * Revision 2.4  2000/01/11  11:13:59  eric
 * Changement de nom pour la base vectorielle : base --> triad
 *
 * Revision 2.3  2000/01/10  17:23:07  eric
 * Modif affichage.
 * Methode fait_derive_cov : ajout de
 *   assert( metre.gamma().get_base() == base )
 * Methode set_std_base : ajout de
 *    assert( *base == mp->get_bvect_cart() )
 *
 * Revision 2.2  2000/01/10  15:15:26  eric
 * Ajout du membre base (base vectorielle sur laquelle sont definies
 *   les composantes).
 *
 * Revision 2.1  1999/12/09  12:39:23  phil
 * changement prototypage des derivees
 *
 * Revision 2.0  1999/12/02  17:18:31  phil
 * *** empty log message ***
 *
 *
 * $Header: /cvsroot/Lorene/C++/Source/Tenseur/tenseur.C,v 1.15 2014/10/13 08:53:41 j_novak Exp $
 *
 */

// Headers C
#include <cstdlib>
#include <cassert>
#include <cmath>

// Headers Lorene
#include "tenseur.h"
#include "metrique.h"
#include "utilitaires.h"

			//--------------//
			// Constructors //
			//--------------//
// Consistency check for tensor densities
//---------------------------------------
namespace Lorene {
bool Tenseur::verif() const {
  return ( (poids == 0.) || (metric != 0x0) ) ;
}

void Tenseur::new_der_met() {
    met_depend = new const Metrique*[N_MET_MAX] ;
    p_derive_cov = new Tenseur*[N_MET_MAX] ;
    p_derive_con = new Tenseur*[N_MET_MAX] ;
    p_carre_scal = new Tenseur*[N_MET_MAX] ;
    for (int i=0; i<N_MET_MAX; i++) {
      met_depend[i] = 0x0 ;
    }
    set_der_0x0() ;
}

// Constructor for a scalar field
// ------------------------------
Tenseur::Tenseur (const Map& map, const Metrique* met, double weight) : 
		mp(&map), valence(0), triad(0x0),
		type_indice(0), n_comp(1), etat(ETATNONDEF), poids(weight),
		metric(met) {
    
  //    assert(verif()) ;
    c = new Cmp*[n_comp] ;
    c[0] = 0x0 ;
    new_der_met() ;
}



// Constructor for a scalar field and from a {\tt Cmp} 
// ---------------------------------------------------
Tenseur::Tenseur (const Cmp& ci, const Metrique* met, double weight) : 
		mp(ci.get_mp()), valence(0), triad(0x0),
		type_indice(0), n_comp(1), etat(ci.get_etat()), poids(weight),
		metric(met){
    
    assert(ci.get_etat() != ETATNONDEF) ; 
    assert(verif()) ;
    
    c = new Cmp*[n_comp] ;

    if ( ci.get_etat() != ETATZERO ) {
      assert( ci.get_etat() == ETATQCQ ) ; 
      c[0] = new Cmp(ci) ;
    }
    else {
      c[0] = 0x0 ;
    }
    new_der_met() ;
}

// Standard constructor 
// --------------------
Tenseur::Tenseur(const Map& map, int val, const Itbl& tipe, 
		 const Base_vect& triad_i, const Metrique* met, double weight) 
		: mp(&map), valence(val), triad(&triad_i), type_indice(tipe), 
		   n_comp(int(pow(3., val))), etat(ETATNONDEF), poids(weight),
		metric(met){
		
    // Des verifs :
    assert (valence >= 0) ;
    assert (tipe.get_ndim() == 1) ;
    assert (valence == tipe.get_dim(0)) ;
    for (int i=0 ; i<valence ; i++)
	assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
    assert(verif()) ;
    
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++)
	c[i] = 0x0 ;
    new_der_met() ;
}

// Standard constructor with the triad passed as a pointer
// -------------------------------------------------------
Tenseur::Tenseur(const Map& map, int val, const Itbl& tipe, 
		 const Base_vect* triad_i, const Metrique* met, double weight) 
		: mp(&map), valence(val), triad(triad_i), type_indice(tipe), 
		   n_comp(int(pow(3., val))), etat(ETATNONDEF), poids(weight),
		metric(met){
		
    // Des verifs :
    assert (valence >= 0) ;
    assert (tipe.get_ndim() == 1) ;
    assert (valence == tipe.get_dim(0)) ;
    for (int i=0 ; i<valence ; i++)
	assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
    //   assert(verif()) ;
    
    if (valence == 0) {	    // particular case of a scalar 
	triad = 0x0 ; 
    }   
    
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++)
	c[i] = 0x0 ;
    new_der_met() ;
}




// Standard constructor when all the indices are of the same type
// --------------------------------------------------------------
Tenseur::Tenseur(const Map& map, int val, int tipe, const Base_vect& triad_i,
		 const Metrique* met, double weight) 
		: mp(&map), valence(val), triad(&triad_i), type_indice(val), 
                  n_comp(int(pow(3., val))), etat (ETATNONDEF), poids(weight), 
		  metric(met){
    
    // Des verifs :
    assert (valence >= 0) ;
    assert ((tipe == COV) || (tipe == CON)) ;
    assert(verif()) ;
    type_indice.set_etat_qcq() ;
    type_indice = tipe ;
    
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++)
	c[i] = 0x0 ;
    new_der_met() ;
}	
	
// Copy constructor
// ----------------
Tenseur::Tenseur (const Tenseur& source) : 
    mp(source.mp), valence(source.valence), triad(source.triad), 
    type_indice(source.type_indice), etat (source.etat), poids(source.poids),
    metric(source.metric) {
  
  //   assert(verif()) ;
    
    n_comp = int(pow(3., valence)) ;
        
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++) {
	int place_source = source.donne_place(donne_indices(i)) ;
	if (source.c[place_source] == 0x0)
	    c[i] = 0x0 ;
	else
	    c[i] = new Cmp(*source.c[place_source]) ;
    }

    assert(source.met_depend != 0x0) ;
    assert(source.p_derive_cov != 0x0) ;
    assert(source.p_derive_con != 0x0) ;
    assert(source.p_carre_scal != 0x0) ;
    new_der_met() ;
    
    if (source.p_gradient != 0x0)
	    p_gradient = new Tenseur (*source.p_gradient) ;
    
    if (source.p_gradient_spher != 0x0)
	    p_gradient_spher = new Tenseur (*source.p_gradient_spher) ;

    for (int i=0; i<N_MET_MAX; i++) {
      met_depend[i] = source.met_depend[i] ;
      if (met_depend[i] != 0x0) {
	
	set_dependance (*met_depend[i]) ;
	
	if (source.p_derive_cov[i] != 0x0)
	  p_derive_cov[i] = new Tenseur (*source.p_derive_cov[i]) ;
	if (source.p_derive_con[i] != 0x0)
	  p_derive_con[i] = new Tenseur (*source.p_derive_con[i]) ;
	if (source.p_carre_scal[i] != 0x0)
	    p_carre_scal[i] = new Tenseur (*source.p_carre_scal[i]) ;
      }
    }
}   

// Constructor from a symmetric tensor
// -----------------------------------
Tenseur::Tenseur (const Tenseur_sym& source) :
	mp(source.mp), valence(source.valence), triad(source.triad), 
	type_indice(source.type_indice), etat(source.etat), 
	poids(source.poids), metric(source.metric) {
    
    assert(verif()) ;
    n_comp = int(pow(3., valence)) ;
        
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++) {
	int place_source = source.donne_place(donne_indices(i)) ;
	if (source.c[place_source] == 0x0)
	    c[i] = 0x0 ;
	else
	    c[i] = new Cmp(*source.c[place_source]) ;
    }

    assert(source.met_depend != 0x0) ;
    assert(source.p_derive_cov != 0x0) ;
    assert(source.p_derive_con != 0x0) ;
    assert(source.p_carre_scal != 0x0) ;
    new_der_met() ;
    
    if (source.p_gradient != 0x0)
	p_gradient = new Tenseur_sym (*source.p_gradient) ;

    for (int i=0; i<N_MET_MAX; i++) {
      met_depend[i] = source.met_depend[i] ;
      if (met_depend[i] != 0x0) {
	
	set_dependance (*met_depend[i]) ;
	
	if (source.p_derive_cov[i] != 0x0)
	  p_derive_cov[i] = new Tenseur (*source.p_derive_cov[i]) ;
	if (source.p_derive_con[i] != 0x0)
	  p_derive_con[i] = new Tenseur (*source.p_derive_con[i]) ;
	if (source.p_carre_scal[i] != 0x0)
	    p_carre_scal[i] = new Tenseur (*source.p_carre_scal[i]) ;
      }
    }
    
}

// Constructor from a file
// -----------------------
Tenseur::Tenseur(const Map& mapping, const Base_vect& triad_i, FILE* fd, 
		 const Metrique* met)
		 : mp(&mapping), triad(&triad_i), type_indice(fd), 
                   metric(met) {
   
    fread_be(&valence, sizeof(int), 1, fd) ;

    if (valence != 0) {
	Base_vect* triad_fich = Base_vect::bvect_from_file(fd) ; 
	assert( *triad_fich == *triad) ; 
	delete triad_fich ; 
    }
    else{
	triad = 0x0 ; 
    }
    
    fread_be(&n_comp, sizeof(int), 1, fd) ;
    fread_be(&etat, sizeof(int), 1, fd) ;
    
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++)
	c[i] = 0x0 ;
    if (etat == ETATQCQ)
	for (int i=0 ; i<n_comp ; i++)
	    c[i] = new Cmp(*mp, *mp->get_mg(), fd) ;

    new_der_met() ;
    
    if (met == 0x0) 
      poids = 0. ;
    else
      fread_be(&poids, sizeof(double), 1, fd) ;
}


// Constructor from a file for a scalar field
// ------------------------------------------
Tenseur::Tenseur (const Map& mapping, FILE* fd, const Metrique* met) 
		 : mp(&mapping), type_indice(fd), metric(met){
   
    fread_be(&valence, sizeof(int), 1, fd) ;

    assert(valence == 0) ; 
    
    triad = 0x0 ; 
    
    fread_be(&n_comp, sizeof(int), 1, fd) ;
    
    assert(n_comp == 1) ; 

    fread_be(&etat, sizeof(int), 1, fd) ;
    
    c = new Cmp*[n_comp] ;

    if (etat == ETATQCQ) {
	c[0] = new Cmp(*mp, *mp->get_mg(), fd) ;
    }
    else{
	c[0] = 0x0 ; 
    }
    
    new_der_met() ;

    if (met == 0x0) 
      poids = 0. ;
    else
      fread_be(&poids, sizeof(double), 1, fd) ;
}




// Constructor used by the derived classes
// ---------------------------------------
Tenseur::Tenseur (const Map& map, int val, const Itbl& tipe, int compo, 
		const Base_vect& triad_i, const Metrique* met, double weight) :
     mp(&map), valence(val), triad(&triad_i), type_indice(tipe), n_comp(compo), 
	    etat (ETATNONDEF), poids(weight), metric(met) {
     
    // Des verifs :
    assert (valence >= 0) ;
    assert (tipe.get_ndim() == 1) ;
    assert (n_comp > 0) ;   
    assert (valence == tipe.get_dim(0)) ;
    for (int i=0 ; i<valence ; i++)
	assert ((tipe(i) == COV) || (tipe(i) == CON)) ;
    //assert(verif()) ;
    
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++)
	c[i] = 0x0 ;
    
    new_der_met() ;
}

// Constructor used by the derived classes when all the indices are of 
// the same type.
// -------------------------------------------------------------------
Tenseur::Tenseur (const Map& map, int val, int tipe, int compo, 
		const Base_vect& triad_i, const Metrique* met, double weight) :
     mp(&map), valence(val), triad(&triad_i), type_indice(val), n_comp(compo), 
	    etat (ETATNONDEF), poids(weight), metric(met) {
    // Des verifs :
    assert (valence >= 0) ;
    assert (n_comp >= 0) ;
    assert ((tipe == COV) || (tipe == CON)) ;
    //assert(verif()) ;
    type_indice.set_etat_qcq() ;
    type_indice = tipe ;
    
    c = new Cmp*[n_comp] ;
    for (int i=0 ; i<n_comp ; i++)
	c[i] = 0x0 ;

    new_der_met() ;
}

			//--------------//
			//  Destructor  //
			//--------------//


Tenseur::~Tenseur () {
    
    del_t() ;
    delete [] met_depend ;
    delete [] p_derive_cov ;
    delete [] p_derive_con ;
    delete [] p_carre_scal ;
    delete [] c ;
}



void Tenseur::del_t() {
    del_derive() ;
    for (int i=0 ; i<n_comp ; i++)
	if (c[i] != 0x0) {
	    delete c[i] ;
	    c[i] = 0x0 ;
	    }
}

void Tenseur::del_derive_met(int j) const {

  assert( (j>=0) && (j<N_MET_MAX) ) ;
  // On gere la metrique ...
  if (met_depend[j] != 0x0) {
    for (int i=0 ; i<N_DEPEND ; i++)
      if (met_depend[j]->dependances[i] == this)
	met_depend[j]->dependances[i] = 0x0 ;
    if (p_derive_cov[j] != 0x0)
      delete p_derive_cov[j] ;
    if (p_derive_con[j] != 0x0)
      delete p_derive_con[j] ;
    if (p_carre_scal[j] != 0x0)
      delete p_carre_scal[j] ;
    set_der_met_0x0(j) ;
  }
}


void Tenseur::del_derive () const {
  for (int i=0; i<N_MET_MAX; i++) 
    del_derive_met(i) ;
  if (p_gradient != 0x0)
    delete p_gradient ;
  if (p_gradient_spher != 0x0)
    delete p_gradient_spher ;
  set_der_0x0() ;
}

void Tenseur::set_der_met_0x0(int i) const {
  met_depend[i] = 0x0 ;
    p_derive_cov[i] = 0x0 ;
    p_derive_con[i] = 0x0 ;
    p_carre_scal[i] = 0x0 ;
}


void Tenseur::set_der_0x0() const {
  for (int i=0; i<N_MET_MAX; i++) 
    set_der_met_0x0(i) ;
  p_gradient = 0x0 ;   
  p_gradient_spher = 0x0 ;   
}

int Tenseur::get_place_met(const Metrique& metre) const {
  int resu = -1 ;
  for (int i=0; i<N_MET_MAX; i++) 
    if (met_depend[i] == &metre) {
      assert(resu == -1) ;
      resu = i ;
    }
  return resu ;
}

void Tenseur::set_dependance (const Metrique& met) const {
    
  int nmet = 0 ;
  bool deja = false ;
  for (int i=0; i<N_MET_MAX; i++) {
    if (met_depend[i] == &met) deja = true ;
    if ((!deja) && (met_depend[i] != 0x0)) nmet++ ;
  }
  if (nmet == N_MET_MAX) {
    cout << "Too many metrics in Tenseur::set_dependances" << endl ;
    abort() ;
  }
  if (!deja) { 
    int conte = 0 ;
    while ((conte < N_DEPEND) && (met.dependances[conte] != 0x0))
      conte ++ ;
    
    if (conte == N_DEPEND) {
      cout << "Too many dependancies in Tenseur::set_dependances " << endl ;
      abort() ;
    }
    else {
      met.dependances[conte] = this ;
      met_depend[nmet] = &met ;
    }
  }
}

void Tenseur::set_etat_qcq() { 
    
    del_derive() ;
    for (int i=0 ; i<n_comp ; i++)
	if (c[i] == 0x0)
	    c[i] = new Cmp(mp) ;
    etat = ETATQCQ ;
}

void Tenseur::set_etat_zero() { 
    del_t() ;
    etat = ETATZERO ;
}

void Tenseur::set_etat_nondef() { 
    del_t() ;
    etat = ETATNONDEF ;
}

// Allocates everything
// --------------------
void Tenseur::allocate_all() {
    
	set_etat_qcq() ; 
	for (int i=0 ; i<n_comp ; i++) {
	    c[i]->allocate_all() ; 
	}
	
} 



void Tenseur::change_triad(const Base_vect& bi) {
    
    bi.change_basis(*this) ; 
    
}

void Tenseur::set_triad(const Base_vect& bi) {
    
    triad = &bi ; 
    
}

void Tenseur::set_poids(double weight) {

    poids = weight ;
}

void Tenseur::set_metric(const Metrique& met) {

    metric = &met ;
}

int Tenseur::donne_place (const Itbl& idx) const {
    
    assert (idx.get_ndim() == 1) ;
    assert (idx.get_dim(0) == valence) ;
    
    for (int i=0 ; i<valence ; i++)
	assert ((idx(i)>=0) && (idx(i)<3)) ;
    int res = 0 ;
    for (int i=0 ; i<valence ; i++)
        res = 3*res+idx(i) ;
    
    return res;
}

Itbl Tenseur::donne_indices (int place) const {
    
    assert ((place >= 0) && (place < n_comp)) ;

    Itbl res(valence) ;
    res.set_etat_qcq() ;
    	    
    for (int i=valence-1 ; i>=0 ; i--) {
	res.set(i) = div(place, 3).rem ;
	place = int((place-res(i))/3) ;
	}
    return res ;
}

void Tenseur::operator=(const Tenseur& t) {
    
    assert (valence == t.valence) ;

    triad = t.triad ; 
    poids = t.poids ;
    metric = t.metric ;

    for (int i=0 ; i<valence ; i++)
	assert (t.type_indice(i) == type_indice(i)) ;
	
    switch (t.etat) {
	case ETATNONDEF: {
	    set_etat_nondef() ;
	    break ;
	}
	
	case ETATZERO: {
	    set_etat_zero() ;
	    break ;
	}
	
	case ETATQCQ: {
	    set_etat_qcq() ;
	    for (int i=0 ; i<n_comp ; i++) {
		int place_t = t.donne_place(donne_indices(i)) ;
		if (t.c[place_t] == 0x0)
		    c[i] = 0x0 ;
		else 
		    *c[i] = *t.c[place_t] ;
	    }
	    break ;
	}
	
	default: {
	    cout << "Unknown state in Tenseur::operator= " << endl ;
	    abort() ;
	    break ;
	    }
    }
}


void Tenseur::operator=(const Cmp& ci) {
    
    assert (valence == 0) ;
    poids = 0. ;
    metric = 0x0 ;

    switch (ci.get_etat()) {
	case ETATNONDEF: {
	    set_etat_nondef() ;
	    break ;
	}
	
	case ETATZERO: {
	    set_etat_zero() ;
	    break ;
	}
	
	case ETATQCQ: {
	    set_etat_qcq() ;
	    *(c[0]) = ci ; 
	    break ;
	}
	
	default: {
	    cout << "Unknown state in Tenseur::operator= " << endl ;
	    abort() ;
	    break ;
	}
    }
}

void Tenseur::operator=(double x) {

    poids = 0. ;
    metric = 0x0 ;
    if (x == double(0)) {
	set_etat_zero() ;
    }
    else {
	assert(valence == 0) ; 
	set_etat_qcq() ;
	*(c[0]) = x ; 
    }

}

void Tenseur::operator=(int x) {

    poids = 0. ;
    metric = 0x0 ;
    if (x == 0) {
	set_etat_zero() ;
    }
    else {
	assert(valence == 0) ; 
	set_etat_qcq() ;
	*(c[0]) = x ; 
    }

}


// Affectation d'un scalaire ...
Cmp& Tenseur::set () {
    
    del_derive() ;
    assert(etat == ETATQCQ) ;
    assert (valence == 0) ;  
    return *c[0] ;
}


// Affectation d'un vecteur :
Cmp& Tenseur::set (int ind) {
    
    del_derive() ;
    assert(valence == 1) ;
    assert (etat == ETATQCQ) ;
    assert ((ind >= 0) && (ind < 3)) ;
    
    return *c[ind] ;
}

// Affectation d'un tenseur d'ordre 2 :
Cmp& Tenseur::set (int ind1, int ind2) {
    
    del_derive() ;
    assert (valence == 2) ;
    assert (etat == ETATQCQ) ;
    assert ((ind1 >= 0) && (ind1 < 3)) ;
    assert ((ind2 >= 0) && (ind2 < 3)) ;
    
    Itbl ind (valence) ;
    ind.set_etat_qcq() ;
    ind.set(0) = ind1 ;
    ind.set(1) = ind2 ;
    
    int place = donne_place(ind) ;
    
    return *c[place] ;
}

// Affectation d'un tenseur d'ordre 3 :
Cmp& Tenseur::set (int ind1, int ind2, int ind3) {
    
    del_derive() ;
    assert (valence == 3) ;
    assert (etat == ETATQCQ) ;
    assert ((ind1 >= 0) && (ind1 < 3)) ;
    assert ((ind2 >= 0) && (ind2 < 3)) ;
    assert ((ind3 >= 0) && (ind3 < 3)) ;
    
    Itbl indices(valence) ;
    indices.set_etat_qcq() ;
    indices.set(0) = ind1 ;
    indices.set(1) = ind2 ;
    indices.set(2) = ind3 ;
    int place = donne_place(indices) ;
 
    return *c[place] ;
}

// Affectation cas general
Cmp& Tenseur::set(const Itbl& indices) {
    
    assert (indices.get_ndim() == 1) ;
    assert (indices.get_dim(0) == valence) ;
    
    del_derive() ;
    assert (etat == ETATQCQ) ;
    for (int i=0 ; i<valence ; i++)
	assert ((indices(i)>=0) && (indices(i)<3)) ;
	
    int place = donne_place(indices) ;
    
    return *c[place] ;
}

// Annulation dans des domaines
void Tenseur::annule(int l) {
    
    annule(l, l) ;     
}

void Tenseur::annule(int l_min, int l_max) {
    
    // Cas particulier: annulation globale : 
    if ( (l_min == 0) && (l_max == mp->get_mg()->get_nzone()-1) ) {
	set_etat_zero() ;
	return ; 
    }
    
    assert( etat != ETATNONDEF ) ; 
    
    if ( etat == ETATZERO ) {
	return ;		// rien n'a faire si c'est deja zero
    }
    else {
	assert( etat == ETATQCQ ) ;	// sinon...
	
	// Annulation des composantes:
	for (int i=0 ; i<n_comp ; i++) {
	    c[i]->annule(l_min, l_max) ; 
	}
	
	// Annulation des membres derives
	if (p_gradient != 0x0) p_gradient->annule(l_min, l_max) ;
	if (p_gradient_spher != 0x0) p_gradient_spher->annule(l_min, l_max) ;
	for (int j=0; j<N_MET_MAX; j++) {
	  if (p_derive_cov[j] != 0x0) p_derive_cov[j]->annule(l_min, l_max) ;
	  if (p_derive_con[j] != 0x0) p_derive_con[j]->annule(l_min, l_max) ;
	  if (p_carre_scal[j] != 0x0) p_carre_scal[j]->annule(l_min, l_max) ;
	}

    }
    
}




// Exctraction :
const Cmp& Tenseur::operator()() const {
    
    assert(valence == 0) ;
    
    if (etat == ETATQCQ) return *c[0] ;	    // pour la performance,
					    // ce cas est traite en premier,
					    // en dehors du switch
    switch (etat) {
	
	case ETATNONDEF : {
	    cout << "Undefined Tensor in Tenseur::operator() ..." << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	    
	case ETATZERO : {
	    return mp->cmp_zero() ;
	    }
	    
	    
	default : {
	    cout <<"Unknown state in Tenseur::operator()" << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	}
}


const Cmp& Tenseur::operator() (int indice) const {
    
    assert ((indice>=0) && (indice<3)) ;
    assert(valence == 1) ;
    
    if (etat == ETATQCQ) return *c[indice] ;	 // pour la performance,
					    // ce cas est traite en premier,
					    // en dehors du switch
    switch (etat) {
	
	case ETATNONDEF : {
	    cout << "Undefined Tensor in Tenseur::operator(int) ..." << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	    
	case ETATZERO : {
	    return mp->cmp_zero() ;
	    }
	    	    
	default : {
	    cout <<"Unknown state in Tenseur::operator(int)" << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	}
}

const Cmp& Tenseur::operator() (int indice1, int indice2) const {
    
    assert ((indice1>=0) && (indice1<3)) ;
    assert ((indice2>=0) && (indice2<3)) ;
    assert(valence == 2) ;
    
    if (etat == ETATQCQ) {		// pour la performance,
	    Itbl idx(2) ;		// ce cas est traite en premier,
	    idx.set_etat_qcq() ;	// en dehors du switch
	    idx.set(0) = indice1 ;
	    idx.set(1) = indice2 ;
	    return *c[donne_place(idx)] ;	
    }

    switch (etat) {
	
	case ETATNONDEF : {
	    cout << "Undefined Tensor in Tenseur::operator(int, int) ..." << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	    
	case ETATZERO : {
	    return mp->cmp_zero() ;
	    }
	   	    
	default : {
	    cout <<"Unknown state in Tenseur::operator(int, int)" << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	} 
}

const Cmp& Tenseur::operator() (int indice1, int indice2, int indice3) const {
    
    assert ((indice1>=0) && (indice1<3)) ;
    assert ((indice2>=0) && (indice2<3)) ;
    assert ((indice3>=0) && (indice3<3)) ;
    assert(valence == 3) ;
    
    if (etat == ETATQCQ) {		// pour la performance,
	    Itbl idx(3) ;		// ce cas est traite en premier,
	    idx.set_etat_qcq() ;	// en dehors du switch
	    idx.set(0) = indice1 ;
	    idx.set(1) = indice2 ;
	    idx.set(2) = indice3 ;
	    return *c[donne_place(idx)] ;
    }

    switch (etat) {
	
	case ETATNONDEF : {
	    cout << "Undefined Tensor in Tenseur::operator(int, int, int) ..." << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	    
	case ETATZERO : {
	    return mp->cmp_zero() ;
	    }
	    	    
	default : {
	    cout <<"Unknown state in Tenseur::operator(int, int, int)" << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	}
}


const Cmp& Tenseur::operator() (const Itbl& indices) const {
    
    assert (indices.get_ndim() == 1) ;
    assert (indices.get_dim(0) == valence) ;
    for (int i=0 ; i<valence ; i++)
	assert ((indices(i)>=0) && (indices(i)<3)) ;
    
    if (etat == ETATQCQ) {		    // pour la performance,
	return *c[donne_place(indices)]	;   // ce cas est traite en premier,
    }					    // en dehors du switch

    switch (etat) {
	
	case ETATNONDEF : {
	    cout << "Undefined Tensor in Tenseur::operator(const Itbl&) ..." << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	    
	case ETATZERO : {
	    return mp->cmp_zero() ;
	    }
	    
	default : {
	    cout <<"Unknown state in Tenseur::operator(const Itbl& )" << endl ;
	    abort() ;
	    return *c[0] ;  // bidon pour satisfaire le compilateur
	    }
	}
    
}

// Gestion de la ZEC :
void Tenseur::dec_dzpuis() {
    
    if (etat == ETATZERO) {
	return ; 
    }
    
    assert(etat == ETATQCQ) ;
   
    for (int i=0 ; i<n_comp ; i++)
	if (c[i] != 0x0)
	    c[i]->dec_dzpuis() ;
}

void Tenseur::inc_dzpuis() {
    
    if (etat == ETATZERO) {
	return ; 
    }

    assert(etat == ETATQCQ) ;
    
    for (int i=0 ; i<n_comp ; i++)
	if (c[i] != 0x0)
	    c[i]->inc_dzpuis() ;
}

void Tenseur::dec2_dzpuis() {
    
    if (etat == ETATZERO) {
	return ; 
    }
    
    assert(etat == ETATQCQ) ;
   
    for (int i=0 ; i<n_comp ; i++)
	if (c[i] != 0x0)
	    c[i]->dec2_dzpuis() ;
}

void Tenseur::inc2_dzpuis() {
    
    if (etat == ETATZERO) {
	return ; 
    }

    assert(etat == ETATQCQ) ;
    
    for (int i=0 ; i<n_comp ; i++)
	if (c[i] != 0x0)
	    c[i]->inc2_dzpuis() ;
}

void Tenseur::mult_r_zec() {
    
    if (etat == ETATZERO) {
	return ; 
    }

    assert(etat == ETATQCQ) ;
    
    for (int i=0 ; i<n_comp ; i++) 
    	if (c[i] != 0x0)
	    c[i]->mult_r_zec() ;
}

// Gestion des bases spectrales (valence <= 2)
void Tenseur::set_std_base() {
    
    if (etat == ETATZERO) {
	return ; 
    }
    
    assert(etat == ETATQCQ) ;
    switch (valence) {
	
	case 0 : {
	    c[0]->std_base_scal() ;
	    break ;
	}
	    
	case 1 : {

	  if ( triad->identify() == (mp->get_bvect_cart()).identify() ) {

	    Base_val** bases = mp->get_mg()->std_base_vect_cart() ;

	    for (int i=0 ; i<3 ; i++)
		(c[i]->va).set_base( *bases[i] ) ;
	    for (int i=0 ; i<3 ; i++)
		delete bases[i] ;
	    delete [] bases ;
	  }
	  else {
	    assert( triad->identify() == (mp->get_bvect_spher()).identify()) ;
	    Base_val** bases = mp->get_mg()->std_base_vect_spher() ;

	    for (int i=0 ; i<3 ; i++)
		(c[i]->va).set_base( *bases[i] ) ;
	    for (int i=0 ; i<3 ; i++)
		delete bases[i] ;
	    delete [] bases ;
	  }
	  break ;
	    
	}
	    
	case 2 : {

	  if( triad->identify() == (mp->get_bvect_cart()).identify() ) {

	    Base_val** bases = mp->get_mg()->std_base_vect_cart() ;
	    
	    Itbl indices (2) ;
	    indices.set_etat_qcq() ;
	    for (int i=0 ; i<n_comp ; i++) {   
		indices = donne_indices(i) ;
		(c[i]->va).set_base( (*bases[indices(0)]) * 
				     (*bases[indices(1)]) ) ;
	    }
	    for (int i=0 ; i<3 ; i++)
		delete bases[i] ;
	    delete [] bases ;
	  }
	  else {
	    assert( triad->identify() == (mp->get_bvect_spher()).identify()) ;
	    Base_val** bases = mp->get_mg()->std_base_vect_spher() ;
	    
	    Itbl indices (2) ;
	    indices.set_etat_qcq() ;
	    for (int i=0 ; i<n_comp ; i++) {   
		indices = donne_indices(i) ;
		(c[i]->va).set_base( (*bases[indices(0)]) * 
				     (*bases[indices(1)]) ) ;
	    }
	    for (int i=0 ; i<3 ; i++)
		delete bases[i] ;
	    delete [] bases ;
	  }
	  break ;
	}
	   
	    default : {
		cout << 
		"Tenseur::set_std_base() : the case valence = " << valence
		 << " is not treated !" << endl ;
		abort() ;
		break ;
	    }
	}
}

 // Le cout :
ostream& operator<<(ostream& flux, const Tenseur &source ) {
    
    flux << "Valence : " << source.valence << endl ;
    if (source.get_poids() != 0.) 
      flux << "Tensor density of weight " << source.poids << endl ;

    if (source.get_triad() != 0x0) {
	flux << "Vectorial basis (triad) on which the components are defined :" 
	     << endl ; 
	flux << *(source.get_triad()) << endl ;
    }
    
    if (source.valence != 0)
	flux << "Type of the indices : " << endl ;
    for (int i=0 ; i<source.valence ; i++) {
	flux << "Index " << i << " : " ;
	if (source.type_indice(i) == CON)
	    flux << " contravariant." << endl ;
	else
	    flux << " covariant." << endl ;
	}
    
    switch (source.etat) {
	
	case ETATZERO : {
	    flux << "Null Tenseur. " << endl ;
	    break ;
	    }
	
	case ETATNONDEF : {
	    flux << "Undefined Tenseur. " << endl ;
	    break ;
	    }
	
	case ETATQCQ : {
	    for (int i=0 ; i<source.n_comp ; i++) {

		Itbl num_indices (source.donne_indices(i)) ;
		flux << "Component " ;
		
		if (source.valence != 0) {
		for (int j=0 ; j<source.valence ; j++)
		    flux << "  " << num_indices(j) ;
		    }
		else
		    flux << "  " << 0 ;
		flux << " : " << endl ;
		flux << "-------------" << endl ; 


		if (source.c[i] != 0x0)
		    flux << *source.c[i] << endl ;
		else
		    flux << "Unknown component ... " << endl ;
		    
	    }
	    break ;
	    }
	default : {
	    cout << "Unknown case in operator<< (ostream&, const Tenseur&)" << endl ;
	    abort() ;
	    break ;
	}
    }
    
    flux << " -----------------------------------------------------" << endl ;
    return flux ;
}

void Tenseur::sauve(FILE* fd) const {
    
    type_indice.sauve(fd) ;	// type des composantes
    fwrite_be(&valence, sizeof(int), 1, fd) ;    // la valence
    
    if (valence != 0) {
	triad->sauve(fd) ;	    // Vectorial basis
    }
    
    fwrite_be(&n_comp, sizeof(int), 1, fd) ; // nbre composantes
    fwrite_be(&etat, sizeof(int), 1, fd) ; // etat
   
    if (etat == ETATQCQ)
	for (int i=0 ; i<n_comp ; i++)
	    c[i]->sauve(fd) ;
    if (poids != 0.)
      fwrite_be(&poids, sizeof(double), 1, fd) ; //poids, si pertinent
}


void Tenseur::fait_gradient () const {
    
    assert (etat != ETATNONDEF) ;
    
    if (p_gradient != 0x0)
	return ;
    else {
 
	// Construction du resultat :
	Itbl tipe (valence+1) ;
	tipe.set_etat_qcq() ;
	tipe.set(0) = COV ;
	for (int i=0 ; i<valence ; i++)
	    tipe.set(i+1) = type_indice(i) ;
	
	// Vectorial basis
	// ---------------
	if (valence != 0) {
	  //	    assert (*triad == mp->get_bvect_cart()) ;
	}

	p_gradient = new Tenseur(*mp, valence+1, tipe, mp->get_bvect_cart(), 
				 metric, poids) ;
    
	if (etat == ETATZERO)
	    p_gradient->set_etat_zero() ;
	else {
	    p_gradient->set_etat_qcq() ;
	    // Boucle sur les indices :
	
	    Itbl indices_source(valence) ;
	    indices_source.set_etat_qcq() ;
	
	    for (int j=0 ; j<p_gradient->n_comp ; j++) {    
		
		Itbl indices_res(p_gradient->donne_indices(j)) ;

		for (int m=0 ; m<valence ; m++)
		    indices_source.set(m) = indices_res(m+1) ;
		 
		p_gradient->set(indices_res) =  
		    (*this)(indices_source).deriv(indices_res(0)) ;
		}
	  }
    }
}

void Tenseur::fait_gradient_spher () const {
    
    assert (etat != ETATNONDEF) ;
    
    if (p_gradient_spher != 0x0)
	return ;
    else {
 
	// Construction du resultat :
	
	if (valence != 0) {
	    cout << 
	    "Tenseur::fait_gradient_spher : the valence must be zero !" 
	    << endl ; 
	    abort() ; 
	}

	p_gradient_spher = new Tenseur(*mp, 1, COV, mp->get_bvect_spher(),
				       metric, poids) ;
    
	if (etat == ETATZERO) {
	    p_gradient_spher->set_etat_zero() ;
	}
	else {
	    assert( etat == ETATQCQ ) ; 
	    p_gradient_spher->set_etat_qcq() ;

	    p_gradient_spher->set(0) = c[0]->dsdr() ;	    // d/dr 
	    p_gradient_spher->set(1) = c[0]->srdsdt() ;	    // 1/r d/dtheta
	    p_gradient_spher->set(2) = c[0]->srstdsdp() ;   // 1/(r sin(theta))
							    //		  d/dphi

	}
    }
}


void Tenseur::fait_derive_cov (const Metrique& metre, int ind) const {
    
  assert (etat != ETATNONDEF) ;
  assert (valence != 0) ;
  
  if (p_derive_cov[ind] != 0x0)
    return ;
  else {
    
    p_derive_cov[ind] = new Tenseur (gradient()) ;
    
    if ((valence != 0) && (etat != ETATZERO)) {

      
      //    assert( *(metre.gamma().get_triad()) == *triad ) ; 
      Tenseur* auxi ;
      for (int i=0 ; i<valence ; i++) {
	
	if (type_indice(i) == COV) {
	  auxi = new Tenseur(contract(metre.gamma(), 0,(*this), i)) ;
	  
	  Itbl indices_gamma(p_derive_cov[ind]->valence) ;
	  indices_gamma.set_etat_qcq() ;
	  //On range comme il faut :
	  for (int j=0 ; j<p_derive_cov[ind]->n_comp ; j++) {
	    
	    Itbl indices (p_derive_cov[ind]->donne_indices(j)) ;
	    indices_gamma.set(0) = indices(0) ;
	    indices_gamma.set(1) = indices(i+1) ;
	    for (int idx=2 ; idx<p_derive_cov[ind]->valence ; idx++)
	      if (idx<=i+1)
		indices_gamma.set(idx) = indices(idx-1) ;
	      else
		indices_gamma.set(idx) = indices(idx) ;
	    
	      p_derive_cov[ind]->set(indices) -= (*auxi)(indices_gamma) ;
	  }
	}   
	else {
	  auxi = new Tenseur(contract(metre.gamma(), 1, (*this), i)) ;
	  
	  Itbl indices_gamma(p_derive_cov[ind]->valence) ;
	  indices_gamma.set_etat_qcq() ;
	  
	  //On range comme il faut :
	  for (int j=0 ; j<p_derive_cov[ind]->n_comp ; j++) {
	    
	    Itbl indices (p_derive_cov[ind]->donne_indices(j)) ;
	    indices_gamma.set(0) = indices(i+1) ;
	    indices_gamma.set(1) = indices(0) ;
	    for (int idx=2 ; idx<p_derive_cov[ind]->valence ; idx++)
	      if (idx<=i+1)
		indices_gamma.set(idx) = indices(idx-1) ;
	      else
		indices_gamma.set(idx) = indices(idx) ;
	    p_derive_cov[ind]->set(indices) += (*auxi)(indices_gamma) ;
	  }
	}
	delete auxi ;
      }
    }
    if ((poids != 0.)&&(etat != ETATZERO)) 
      *p_derive_cov[ind] = *p_derive_cov[ind] - 
	poids*contract(metre.gamma(), 0, 2) * (*this) ;
    
  }
}



void Tenseur::fait_derive_con (const Metrique& metre, int ind) const {
    
  if (p_derive_con[ind] != 0x0)
    return ;
  else {
    // On calcul la derivee covariante :
    if (valence != 0)
      p_derive_con[ind] = new Tenseur
	(contract(metre.con(), 1, derive_cov(metre), 0)) ;
    
    else {
      Tenseur grad (gradient()) ;
      grad.change_triad( *(metre.con().get_triad()) ) ;
      p_derive_con[ind] = new Tenseur (contract(metre.con(), 1, grad, 0)) ;
    }
  }
}

void Tenseur::fait_carre_scal (const Metrique& met, int ind) const {
    
    if (p_carre_scal[ind] != 0x0)
	return ;
    else {
	assert (valence != 0) ;   // A ne pas appeler sur un scalaire ;
       
	// On bouge tous les indices :
	Tenseur op_t(manipule(*this, met)) ;
   
	Tenseur* auxi = new Tenseur(contract(*this, 0, op_t, 0)) ;
	Tenseur* auxi_old ;
    
	// On contracte tous les indices restant :
	for (int indice=1 ; indice<valence ; indice++) {
	    auxi_old = new Tenseur(contract(*auxi, 0, valence-indice)) ;
	    delete auxi ;
	    auxi = new Tenseur(*auxi_old) ;
	    delete auxi_old ;
	}
	p_carre_scal[ind] = new Tenseur (*auxi) ;
	delete auxi ;
    }
}
    
const Tenseur& Tenseur::gradient () const {
    if (p_gradient == 0x0)
	fait_gradient() ;
    return *p_gradient ;
}

const Tenseur& Tenseur::gradient_spher() const {
    if (p_gradient_spher == 0x0)
	fait_gradient_spher() ;
    return *p_gradient_spher ;
}

const Tenseur& Tenseur::derive_cov (const Metrique& metre) const {
    
    if (valence == 0)
	return gradient() ;
    else {
	set_dependance(metre) ;
	int j = get_place_met(metre) ;
	assert(j!=-1) ;
	if (p_derive_cov[j] == 0x0)
	    fait_derive_cov (metre,j) ;
	return *p_derive_cov[j] ;
    }
}

const Tenseur& Tenseur::derive_con (const Metrique& metre) const {
    set_dependance(metre) ;
    int j = get_place_met(metre) ;
    assert(j!=-1) ;
    if (p_derive_con[j] == 0x0)
	fait_derive_con (metre, j) ;
    return *p_derive_con[j] ;
}

const Tenseur& Tenseur::carre_scal (const Metrique& metre) const {
    set_dependance(metre) ;
    int j = get_place_met(metre) ;
    assert(j!=-1) ;
    if (p_carre_scal[j] == 0x0)
	fait_carre_scal (metre, j) ;
    return *p_carre_scal[j] ;
}
}