22// Copyright 2014 by David Wright.
33// All rights reserved.
44
5+ using Meta . Numerics . Matrices ;
56using System ;
67using System . Collections . Generic ;
78using System . Diagnostics ;
@@ -119,8 +120,7 @@ public static bool TryParse (string text, out PermutationAsCycles result) {
119120 int [ ] cycle = new int [ cycleText . Length ] ;
120121 dimension += cycle . Length ;
121122 for ( int k = 0 ; k < cycle . Length ; k ++ ) {
122- int value ;
123- if ( ! Int32 . TryParse ( cycleText [ k ] , out value ) ) return ( false ) ;
123+ if ( ! Int32 . TryParse ( cycleText [ k ] , out int value ) ) return ( false ) ;
124124 cycle [ k ] = value ;
125125 }
126126 cyclesList . Add ( cycle ) ;
@@ -217,8 +217,7 @@ public static bool TryParse (string text, out PermutationAsMap result) {
217217 int [ ] map = new int [ mapText . Length ] ;
218218 bool [ ] flags = new bool [ mapText . Length ] ;
219219 for ( int i = 0 ; i < map . Length ; i ++ ) {
220- int value ;
221- if ( ! Int32 . TryParse ( mapText [ i ] , out value ) ) return ( false ) ;
220+ if ( ! Int32 . TryParse ( mapText [ i ] , out int value ) ) return ( false ) ;
222221 if ( ( value < 0 ) || ( value > ( map . Length - 1 ) ) ) return ( false ) ;
223222 if ( flags [ value ] ) return ( false ) ;
224223 map [ i ] = value ;
@@ -411,9 +410,8 @@ public string ToString (string format, IFormatProvider formatProvider) {
411410 /// <exception cref="FormatException"><paramref name="text"/> is not a valid text representation of a permutation.</exception>
412411 public static Permutation Parse ( string text ) {
413412 if ( text == null ) throw new ArgumentNullException ( nameof ( text ) ) ;
414- Permutation output ;
415- if ( TryParse ( text , out output ) ) {
416- return ( output ) ;
413+ if ( TryParse ( text , out Permutation output ) ) {
414+ return output ;
417415 } else {
418416 throw new FormatException ( ) ;
419417 }
@@ -437,13 +435,11 @@ public static bool TryParse (string text, out Permutation output) {
437435 bool success = false ;
438436 if ( ( text . Length > 0 ) && ( text [ 0 ] == '[' ) ) {
439437 // try to parse as ordering
440- PermutationAsMap map ;
441- success = PermutationAsMap . TryParse ( text , out map ) ;
438+ success = PermutationAsMap . TryParse ( text , out PermutationAsMap map ) ;
442439 if ( success ) output = new Permutation ( map ) ;
443440 } else {
444441 // try to parse as cycles
445- PermutationAsCycles cycles ;
446- success = PermutationAsCycles . TryParse ( text , out cycles ) ;
442+ success = PermutationAsCycles . TryParse ( text , out PermutationAsCycles cycles ) ;
447443 if ( success ) output = new Permutation ( cycles ) ;
448444 }
449445
@@ -494,6 +490,19 @@ private void ComputeMap () {
494490 map = new PermutationAsMap ( FromCyclesToMap ( cycles . cycles ) ) ;
495491 }
496492
493+ internal int [ ] Map {
494+ get {
495+ if ( map == null ) ComputeMap ( ) ;
496+ return map . map ;
497+ }
498+ }
499+
500+ internal int [ ] [ ] Cycles {
501+ get {
502+ if ( cycles == null ) ComputeCycles ( ) ;
503+ return cycles . cycles ;
504+ }
505+ }
497506
498507 /// <summary>
499508 /// Applies the permutation to a list.
@@ -798,6 +807,13 @@ public static Permutation GetRandomPermutation (int dimension, Random rng) {
798807 return ( new Permutation ( PermutationAsMap . GetRandomPermutation ( dimension , rng ) ) ) ;
799808 }
800809
810+ /// <summary>
811+ /// Get a matrix representation of the permuation.
812+ /// </summary>
813+ /// <returns>A matrix representation of the permutation.</returns>
814+ public PermutationMatrix ToMatrix ( ) {
815+ return new PermutationMatrix ( this ) ;
816+ }
801817 }
802818
803819}
0 commit comments