File: misc_functions

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (317 lines) | stat: -rw-r--r-- 8,048 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
#  Copyright (c) 1997-2024
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  This program 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, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program 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.
#-------------------------------------------------------------------------------


# @category Data Conversion
# Change the type of the polymake object to one of its base types
# (aka ancestor in the inheritance hierarchy).
# The object loses all properties that are unknown in the target type.
# @tparam Target  the desired new type
# @param Core::BigObject object to be modified
# @return Core::BigObject  the same //object//, but with modified type

user_function cast<Target>(Core::BigObject) { $_[0]->cast_me(typeof Target); }


# @category Utilities
# Returns the maximal element of an array.
# @param ARRAY array
# @example
# > print maximum([1,2,3,4,5,6,7,23]);
# | 23

user_function maximum($) {
   my ($data)=@_;
   my $max;
   assign_max($max, $_) for @$data;
   return $max;
}


# @category Utilities
# Returns the minimal element of an array.
# @param ARRAY array
# @example
# > print minimum([23,42,666]);
# | 23

user_function minimum($) {
   my ($data)=@_;
   my $min;
   assign_min($min, $_) for @$data;
   return $min;
}


# @category Utilities
# Returns the average value of the array elements.
# @param ARRAY array
# @example
# > print average([1,2,3]);
# | 2

user_function average($) {
   my ($data)=@_;
   my $n=@$data or return;
   my $s=0;
   $s+= $_ for @$data;
   return $s/$n;
}

# @category Utilities
# Returns the sum of the array elements.
# @param ARRAY array
# @example
# > print sum([1,2,3]);
# | 6

user_function sum($) {
   my ($data)=@_;
   my $s=0;
   $s+= $_ for @$data;
   return $s;
}

# @category Utilities
# Returns the median value of the array elements.
# Running time O(n log(n)) if n is the length of the input.                      
# @param ARRAY array
# @example
# > print median([1,2,3,9]);
# | 2.5

user_function median($) {
   my ($data)=@_;
   my $n=@$data or return;
   my @sorted = sort { $a <=> $b } @$data;
   if ($n % 2 == 1) {
      return $sorted[($n-1)/2];
   } else {
      return ($sorted[$n/2-1] + $sorted[$n/2])/2;
   }
}

# @category Utilities
# Produce a histogram of an array: each different element value is mapped on the number of its occurences.
# @param ARRAY data
# @return Map<Any, Int>
# @example
# > $H = histogram([1,1,2,2,2,3,3,2,3,3,1,1,1,3,2,3]);
# > print $H;
# | {(1 5) (2 5) (3 6)}

user_function histogram {
   my ($data)=@_;
   return unless @$data;
   my $element_type=Core::PropertyType::guess_array_element_type($data);
   my $map=new Map<$element_type, Int>();
   ++$map->{$_} for @$data;
   $map
}


# @category Utilities
# Checks if a given sequence is unimodal
# @param ARRAY data
# @return Bool
# @example 
# > print is_unimodal([1,1,2,3,3,2,2,1]);
# | 1                     
# > print is_unimodal([3,3,2,-1]);
# | 1
# > print is_unimodal([1,3,2,3]);
# | 0
user_function is_unimodal {
    my ($seq) = @_;
    return unless @$seq;
    my $seen_decreasing = 0;
    my $prev=$seq->[0];
    for (@$seq) {
        if ($prev < $_) {
            if ($seen_decreasing) {
                return 0;
            }
        } elsif ($prev > $_) {
            $seen_decreasing = 1;
        }
        $prev = $_;
    }
    return 1;
}
                     
# @category Utilities
# Checks if a given sequence is nonnegative
# @param ARRAY data
# @return Bool
# @example 
# > print is_nonnegative([1,0,3]);
# | 1
user_function is_nonnegative {
    my ($seq) = @_;
    return unless @$seq;
    for (@$seq) {
        if ($_ < 0) {
            return 0;
        }
    }
    return 1;
}
                     
# @category Utilities
# Returns the first //m// Fibonacci numbers.
# @param Int m
# @return ARRAY
user_function fibonacci_numbers {
   my ($m) = @_;
   my @numbers;
   if ($m>=1) {
      push @numbers, 1;
      if ($m>=2) {
         push @numbers, 1;
         for (my $i=2; $i<$m; ++$i) {
            push @numbers, $numbers[$i-1]+$numbers[$i-2];
         }
      }
   }
   return @numbers;
}

# Takes (vertex) labels and incidence information to produce new (facet) labels.
function induced_labels(Array, IncidenceMatrix) {
   my ($v_labels, $incidence)=@_;
   new Array<String>( map { join(",", @$v_labels[ @$_ ]) } @$incidence);
}

# @category Utilities
# Find the given labels in an array and return their indices.
# @param Array<String> array
# @param String label label ...
# @return Int Int ...
function labeled_output(Array<String>, $, $) {
    my ($labels, $facets, $size) = @_;
    my $max_label_length=0;
    foreach my $l(@{$labels}) {
	assign_max($max_label_length, length($l));
    }
    my @labeled_facets;
    foreach my $i (0..$size-1) {
	my $lf = "[";
	my $first = 1;
	foreach my $v (@{$facets->[$i]}) {
	    if ($first == 1) {
		$first = 0;
	    } else {
		$lf .= " " unless $max_label_length == 1;
	    }
	    $lf .= $labels->[$v];
	}
	$lf .= "]";
	push @labeled_facets, $lf;
    }
    return \@labeled_facets;
}
                     
# @category Utilities
# Find the given labels in an array and return their indices.
# @param Array<String> array
# @param String label label ...
# @return Int Int ...

function find_labels {
   my $array=shift;
   my %asked;
   my $notfound=@_;
   foreach (@_) {
      $asked{$_}++ and croak( "label $_ occurs twice" );
   }
   my @ret;
   my $i=0;
   foreach (@$array) {
      if (delete $asked{$_}) {
         push @ret, $i;
         --$notfound or last;
      }
      ++$i;
   }
   if ($notfound) {
      if ($notfound>1) {
         croak( "Labels ", join(", ", keys %asked), " do not exist" );
      } else {
         croak( "Label ", keys(%asked), " does not exist" );
      }
   }
   @ret;
}

# @category Utilities
# Return a map indexing an array of sets
# @param Array<Set<Int>> array
# @return HashMap<Array<Set<Int>>, Int>
# @example [nocompare]
# > $s1 = new Set(1,2,3);
# > $s2 = $s1 - 1;
# > $a = new Array<Set>($s1,$s2,$s1);
# > print index_of($a);
# | {({1 2 3} 2) ({2 3} 1)}

user_function index_of(Array<Set<Int>>) {
    my $array = shift;
    my $index_of = new HashMap<Set<Int>, Int>;
    my $i=0;
    foreach (@{$array}) {
        $index_of->{$_} = $i++;
    }
    $index_of;
}


# @category Utilities
# Given a metric, return a triangle matrix whose (i,j)-entry contains the distance between point i and j of the point set //S// for i<j. All entrys below the diagonal are zero. The metric is passed as a perl subroutine mapping two input vectors to a real value.
# @param Matrix S
# @param CODE d
# @return Matrix
# @example
# The following defines the perl subroutine dist as the euclidean metric and then saves the distance matrix of the 3-cubes vertices in the variable $M:
# > sub dist($$) {
# >    my $v = $_[0] - $_[1];
# >    return sqrt(new Float($v*$v)); }
# > $M = distance_matrix(polytope::cube(3)->VERTICES, \&dist);
user_function distance_matrix($$){
	my ($S, $d) = @_;
	my $n = $S->rows;
	my $M = new Matrix($n,$n);
	for (my $i = 0; $i<$n; $i++){
		for (my $j = $i+1; $j<$n; $j++){
			$M->elem($i,$j) = &$d( $S->row($i) , $S->row($j));
		}
	}
	return $M;
                     }

# @category Linear Algebra
# Compute the Hadamard product of two matrices with same dimensions.
# @param Matrix M1
# @param Matrix M2
# @return Matrix"

user_function hadamard_product<Scalar>(Matrix<type_upgrade<Scalar>,_>, Matrix<type_upgrade<Scalar>,_>): c++ (include => "polymake/common/hadamard_product.h");

                     
# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: