SheafSystem  0.0.0.0
tensor_variance.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #include "SheafSystem/tensor_variance.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/std_iostream.h"
24 
25 using namespace std;
26 using namespace fiber_bundle; // Workaround for MS C++ bug.
27 
28 //==============================================================================
29 // TENSOR_VARIANCE FACET
30 //==============================================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36 {
37  // Preconditions:
38 
39  // Body:
40 
41  _p = 0;
42 
43  // Postconditions:
44 
45  ensure(p() == 0);
46 
47  // Exit:
48 
49  return;
50 }
51 
54 {
55  // Preconditions:
56 
57  // Body:
58 
59  *this = xother;
60 
61  // Postconditions:
62 
63  // Exit:
64 
65  return;
66 }
67 
70 {
71  // Preconditions:
72 
73  require((0 <= xp) && (xp < capacity()));
74 
75  // Body:
76 
77  _p = xp;
78 
79  // Postconditions:
80 
81  ensure(p() == xp);
82  ensure_for_all(i, 0, xp, is_contravariant(i));
83 
84  // Exit:
85 
86  return;
87 }
88 
91 {
92  // Preconditions:
93 
94  // Body:
95 
96  // Nothing to do.
97 
98  // Postconditions:
99 
100 
101  // Exit:
102 
103  return;
104 }
105 
109 {
110  // Preconditions:
111 
112 
113  // Body:
114 
115  _p = xother._p;
116  _variance = xother._variance;
117 
118  // Postconditions:
119 
120  ensure((*this) == xother);
121 
122  // Exit:
123 
124  return (*this);
125 }
126 
127 bool
129 operator==(const tensor_variance& xother) const
130 {
131  // Preconditions:
132 
133 
134  // Body:
135 
136  bool result = (_p == xother._p) && (_variance == xother._variance);
137 
138  // Postconditions:
139 
140  // Exit:
141 
142  return result;
143 }
144 
145 
146 int
149 {
150  return numeric_limits<unsigned long int>::digits;
151 }
152 
153 int
155 p() const
156 {
157  return _p;
158 }
159 
160 bool
162 variance(int xi) const
163 {
164  bool result;
165 
166  // Preconditions:
167 
168  require((0 <= xi) && (xi < p()));
169 
170  // Body:
171 
172  result = _variance[xi];
173 
174  // Postconditions:
175 
176 
177  // Exit:
178 
179  return result;
180 }
181 
182 void
184 put_variance(int xi, bool xvalue)
185 {
186  // Preconditions:
187 
188  require((0 <= xi) && (xi < p()));
189 
190  // Body:
191 
192  _variance[xi] = xvalue;
193 
194  // Postconditions:
195 
196  ensure(variance(xi) == xvalue);
197 
198  // Exit:
199 
200  return;
201 }
202 
203 void
205 put_variance(bool xvalue)
206 {
207  // Preconditions:
208 
209  // Body:
210 
211  for(int i=0; i< _p; ++i)
212  {
213  _variance[i] = xvalue;
214  }
215 
216 
217  // Postconditions:
218 
219  ensure_for_all(i, 0, p(), variance(i) == xvalue);
220 
221  // Exit:
222 
223  return;
224 }
225 
226 bool
228 is_covariant(int xi) const
229 {
230  bool result;
231 
232  // Preconditions:
233 
234  require((0 <= xi) && (xi < p()));
235 
236  // Body:
237 
238  result = _variance[xi];
239 
240  // Postconditions:
241 
242  ensure(result == variance(xi));
243 
244  // Exit:
245 
246  return result;
247 }
248 
249 bool
252 {
253  // Preconditions:
254 
255  // Body:
256 
257  bool result = (_p == 0) || (_variance.count() == _p);
258 
259  // Postconditions:
260 
261 
262  // Exit:
263 
264  return result;
265 }
266 
267 bool
269 is_contravariant(int xi) const
270 {
271  bool result;
272 
273  // Preconditions:
274 
275  require((0 <= xi) && (xi < p()));
276 
277  // Body:
278 
279  result = !_variance[xi];
280 
281  // Postconditions:
282 
283  ensure(result == !variance(xi));
284 
285  // Exit:
286 
287  return result;
288 }
289 
290 bool
293 {
294  bool result;
295 
296  // Preconditions:
297 
298  // Body:
299 
300  result = (_p == 0) || (_variance.count() == 0);
301 
302  // Postconditions:
303 
304 
305  // Exit:
306 
307  return result;
308 }
309 
310 bool
312 is_mixed() const
313 {
314  bool result;
315 
316  // Preconditions:
317 
318  // Body:
319 
320  if(_p < 2)
321  {
322  result = false;
323  }
324  else
325  {
326  size_t lct = _variance.count();
327  result = ((0 < lct) && (lct < _p));
328  }
329 
330  // Postconditions:
331 
332 
333  // Exit:
334 
335  return result;
336 }
337 
338 bool
340 is_pure() const
341 {
342  return !is_mixed();
343 }
344 
345 void
348 {
349  // Preconditions:
350 
351  // Body:
352 
353  for(int i=1; i< _p; ++i)
354  {
355  _variance[i] = _variance[0];
356  }
357 
358 
359  // Postconditions:
360 
361  ensure(is_pure());
362  ensure_for_all(i, 0, p(), variance(i) == variance(0));
363 
364  // Exit:
365 
366  return;
367 }
368 
369 // PROTECTED MEMBER FUNCTIONS
370 
371 // PRIVATE MEMBER FUNCTIONS
372 
373 
374 //==============================================================================
375 // NON-MEMBER FUNCTIONS
376 //==============================================================================
377 
380 contract(const tensor_variance& x0, int xp, int xq)
381 {
382 
383  // Preconditions:
384 
385  require((0 <= xp) && (xp < x0.p()));
386  require((0 <= xq) && (xq < x0.p()));
387 
388  // Body:
389 
390  int p = x0.p();
391 
392  tensor_variance result(p-2);
393 
394  int i = 0;
395  for(int j=0; j<p; ++j)
396  {
397  if((j!=xp) && (j!=xq))
398  {
399  result.put_variance(i++, x0.variance(j));
400  }
401  }
402 
403  // Postconditions:
404 
405  ensure(result.p() == x0.p() - 2);
406 
407  // Exit:
408 
409  return result;
410 }
411 
415 {
416 
417  // Preconditions:
418 
419  // Body:
420 
421  int p0 = x0.p();
422  int p1 = x1.p();
423  int pr = p0+p1;
424 
425  tensor_variance result(pr);
426 
427  int r=0;
428  for(int i=0; i<p0; ++i)
429  {
430  result.put_variance(r++, x0.variance(i));
431  }
432 
433  for(int i=0; i<p1; ++i)
434  {
435  result.put_variance(r++, x1.variance(i));
436  }
437 
438  // Postconditions:
439 
440  ensure(result.p() == x0.p() + x1.p());
441  ensure(result.is_covariant() == x0.is_covariant());
442 
443  // Exit:
444 
445  return result;
446 }
447 
451 {
452 
453  // Preconditions:
454 
455  require(x0.is_covariant() || x0.is_contravariant());
456  require(x0.p() >= 1);
457 
458  // Body:
459 
460  int p = x0.p();
461 
462  tensor_variance result(p-1);
463 
464  result.put_variance(x0.is_covariant());
465 
466  // Postconditions:
467 
468  ensure(result.p() == x0.p() - 1);
469  ensure(x0.is_covariant() ? result.is_covariant() : true);
470  ensure(x0.is_contravariant() ? result.is_contravariant() : true);
471 
472  // Exit:
473 
474  return result;
475 }
476 
479 star(const tensor_variance& x0, int xdd)
480 {
481 
482  // Preconditions:
483 
484  require(x0.is_covariant() || x0.is_contravariant());
485  require(x0.p() > 0);
486  require(x0.p() <= xdd);
487 
488  // Body:
489 
490  int p = x0.p();
491 
492  tensor_variance result(xdd - p);
493 
494  result.put_variance(x0.is_covariant());
495 
496  // Postconditions:
497 
498  ensure(result.p() == xdd - x0.p());
499  ensure(x0.is_covariant() ? result.is_covariant() : true);
500  ensure(x0.is_contravariant() ? result.is_contravariant() : true);
501 
502  // Exit:
503 
504  return result;
505 }
506 
509 wedge(const tensor_variance& x0, const tensor_variance& x1)
510 {
511 
512  // Preconditions:
513 
514  require(x0.is_covariant() || x0.is_contravariant());
515  require(x1.is_covariant() || x1.is_contravariant());
516  require(x0.is_covariant() == x1.is_covariant());
517 
518  // Body:
519 
520  int p0 = x0.p();
521  int p1 = x1.p();
522  int pr = p0+p1;
523 
524  tensor_variance result(pr);
525 
526  result.put_variance(x0.is_covariant());
527 
528  // Postconditions:
529 
530  ensure(result.p() == x0.p() + x1.p());
531  ensure(result.is_covariant() == x0.is_covariant());
532 
533  // Exit:
534 
535  return result;
536 }
537 
540 raise(const tensor_variance& x0, int xi)
541 {
542 
543  // Preconditions:
544 
545  require((0 <= xi) && (xi < x0.p()));
546 
547  // Body:
548 
549  tensor_variance result(x0);
550 
551  result.put_variance(xi, false);
552 
553  // Postconditions:
554 
555  ensure(result.p() == x0.p());
556  ensure(result.is_contravariant(xi));
557 
558  // Exit:
559 
560  return result;
561 }
562 
565 lower(const tensor_variance& x0, int xi)
566 {
567 
568  // Preconditions:
569 
570  require((0 <= xi) && (xi < x0.p()));
571 
572  // Body:
573 
574  tensor_variance result(x0);
575 
576  result.put_variance(xi, true);
577 
578  // Postconditions:
579 
580  ensure(result.p() == x0.p());
581  ensure(result.is_covariant(xi));
582 
583  // Exit:
584 
585  return result;
586 }
587 
SHEAF_DLL_SPEC tensor_variance lower(const tensor_variance &x0, int xi)
The variance of the lower of a tensor with variance x0 on index xi.
bool is_pure() const
True if and only if all indices are covariant or all indices are contravariant.
bool is_contravariant() const
True if and only if all indices are contravariant.
~tensor_variance()
Destructor; not virtual, this can not be a base class.
bool operator==(const tensor_variance &xother) const
Equality comparison operator.
int p() const
The tensor degree.
The "type" of a tensor; specifies the degree and the co- or contra-variance for each index of a tenso...
void put_variance(int xi, bool xvalue)
Sets the variance of the xi-th index to xvalue.
tensor_variance & operator=(const tensor_variance &xother)
Assignment operator.
STL namespace.
SHEAF_DLL_SPEC tensor_variance tensor_product(const tensor_variance &x0, const tensor_variance &x1)
The variance of the tensor product of tensors with variance x0 and x1.
static int capacity()
The largest value of p this implementation will support.
bool is_mixed() const
True if and only if there exists at least one index that is covariant and at least one that is contra...
SHEAF_DLL_SPEC tensor_variance raise(const tensor_variance &x0, int xi)
The variance of the raise of a tensor with variance x0 on index xi.
SHEAF_DLL_SPEC tensor_variance hook(const tensor_variance &x0)
The variance of the hook of a tensor with variance x0.
bool is_contravariant(int xi) const
True if and only if the xi-th index is covariant; synonym for !variance(xi).
SHEAF_DLL_SPEC tensor_variance contract(const tensor_variance &x0, int xp, int xq)
The variance of the contration of a tensor with variance x0 on indices xp and xq. ...
tensor_variance()
Default constructor.
bool variance(int xi) const
The variance of the xi-th index; covariant if true, contravariant if false.
void purify()
Sets all indices the same as index 0.
SHEAF_DLL_SPEC tensor_variance wedge(const tensor_variance &x0, const tensor_variance &x1)
The variance of the wedge of a tensor with variance x0 with a tnesor with variance x1...
SHEAF_DLL_SPEC tensor_variance star(const tensor_variance &x0, int xdd)
The variance of the Hodge star of a tensor with variance x0 over a vector space of dimension xdd...
bool is_covariant(int xi) const
True if and only if the xi-th index is covariant; synonym for variance(xi).
Namespace for the fiber_bundles component of the sheaf system.
bool is_covariant() const
True if and only if all indices are covariant.