Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1997
  6. //
  7. // File: marginals.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // marginals.h: Definitions for marginals tables.
  12. //
  13. // See marginals.cpp for documentation
  14. //
  15. #ifndef _MARGINALS_H_
  16. #define _MARGINALS_H_
  17. // Class of multidimensional array capable of intelligent
  18. // marginalization.
  19. class MARGINALS : public MDVCPD
  20. {
  21. friend class MARGSUBITER;
  22. public:
  23. MARGINALS ( const VPGNODEMBN & vpgnd )
  24. { Init( vpgnd ); }
  25. MARGINALS () {}
  26. // Initialize from an array of node pointers
  27. // (discrete only: GNODEMBND)
  28. void Init ( const VPGNODEMBN & vpgnd )
  29. {
  30. _vpgnd = vpgnd;
  31. Init( VimdFromVpgnd( _vpgnd ) );
  32. }
  33. // Allow access to the table of dimensions
  34. const VIMD & Vimd () const
  35. { return Slice().size(); }
  36. const VPGNODEMBN & Vpgnd () const
  37. { return _vpgnd; }
  38. // Marginalize down to a single node
  39. void Marginalize ( GNODEMBND * pgndd, MDVCPD & distd );
  40. // Marginalize down to a subset of our node set using a table of nodes
  41. void Marginalize ( const VPGNODEMBN & vpgndSubset, MARGINALS & marg );
  42. // Marginalize down to a subset of our node set using the other's table of nodes
  43. void Marginalize ( MARGINALS & marg );
  44. // Marginalize to subset using pre-computed iterators
  45. void Marginalize ( MARGINALS & margSubset, Iterator & itSelf, Iterator & itSubset );
  46. // For "absorption", update this sepset marginal from another
  47. void UpdateRatios ( const MARGINALS & marg );
  48. // Multiply corresponding entries in this marginal by those in another
  49. void MultiplyBySubset ( const MARGINALS & margSubset );
  50. // Multiply corresponding entries using precomputed iterators
  51. void MultiplyBySubset ( Iterator & itSelf, Iterator & itSubset );
  52. void Multiply ( REAL r );
  53. void Invert ();
  54. // Construct the complete table of conditional probabilities for a given node
  55. // given a reordering table. Build _vpgnd accordingly.
  56. void CreateOrderedCPDFromNode ( GNODEMBND * pgndd,
  57. const VIMD & vimdFamilyReorder );
  58. void ClampNode ( GNODEMBND * pgndd, const CLAMP & clamp );
  59. // Given a reorder table, return true if it's moot (no reordering present)
  60. static bool BOrdered ( const VIMD & vimdReorder );
  61. // Convert a node table to a dimension array
  62. inline static VIMD VimdFromVpgnd ( const VPGNODEMBN & vpgnd )
  63. {
  64. VIMD vimd( vpgnd.size() );
  65. for ( int i = 0; i < vpgnd.size(); i++ )
  66. {
  67. const GNODEMBND * pgndd;
  68. DynCastThrow( vpgnd[i], pgndd );
  69. vimd[i] = pgndd->CState();
  70. }
  71. return vimd;
  72. }
  73. // Return true if each entry in this marginal is equal the corresponding entry
  74. // in a like-dimensioned other marginal within the stated tolerance
  75. bool BEquivalent ( const MARGINALS & marg, REAL rTolerance = 0.0 );
  76. void Dump();
  77. // Return the signed table of dimensions used for marginalizing
  78. VSIMD VsimdSubset ( const VPGNODEMBN & vpgndSubset );
  79. protected:
  80. // Table of node pointers for each dimension of this marginal
  81. VPGNODEMBN _vpgnd;
  82. protected:
  83. MARGINALS ( const VIMD & vimd )
  84. : MDVCPD( vimd )
  85. {}
  86. // Initialize from a table of dimensions
  87. void Init ( const VIMD & vimd, size_t start = 0 )
  88. { MDVCPD::Init( vimd, start ); }
  89. // Return the table of pseudo-dimensions for marginalizing to a single node
  90. VSIMD VsimdFromNode ( GNODEMBND * pgndd );
  91. void SetUniform ();
  92. void ThrowMisuse ( SZC szcMsg );
  93. // Reorder a single m-d vector subscript array. 'vimdReorder' is the
  94. // table in MARGINALS (topological) sequence of the original dimensions.
  95. inline static
  96. void ReorderVimd ( const VIMD & vimdReorder, const VIMD & vimdIn, VIMD & vimdOut );
  97. // Reorder an array containing a node's family based upon the reordering
  98. // table given.
  99. inline static
  100. void ReorderVimdNodes ( const VIMD & vimdReorder, GNODEMBND * pgndd, VPGNODEMBN & vpgnd );
  101. // Resize the MDVCPD for a UPD for the node
  102. inline static
  103. void ResizeDistribution ( GNODEMBND * pgndd, MDVCPD & distd );
  104. };
  105. // Resize the MDVCPD for a UPD for the node
  106. inline
  107. void MARGINALS :: ResizeDistribution ( GNODEMBND * pgndd, MDVCPD & distd )
  108. {
  109. distd.MDVDENSE::Init( 1, pgndd->CState() );
  110. }
  111. inline
  112. static
  113. ostream & operator << ( ostream & ostr, const VPGNODEMBN & vpgnd )
  114. {
  115. ostr << '[';
  116. for ( int i = 0; i < vpgnd.size(); i++ )
  117. {
  118. const GNODEMBN * pgnd = vpgnd[i];
  119. ostr << pgnd->ZsrefName().Szc();
  120. if ( i + 1 < vpgnd.size() )
  121. ostr << ',';
  122. }
  123. return ostr << ']';
  124. }
  125. #endif // _MARGINALS_H_