| | 2263 | def weighted_degree(self, *weights): |
| | 2264 | """ |
| | 2265 | Return the weighted degree of ``self``, which is the maximum weighted |
| | 2266 | degree of all monomials in ``self``; the weighted degree of a monomial |
| | 2267 | is the sum of all powers of the variables in the monomial, each power |
| | 2268 | multiplied with its respective weight in ``weights''. |
| | 2269 | |
| | 2270 | TODO: This implementation is slow. It should be reimplemented to use |
| | 2271 | the Singular interface. |
| | 2272 | |
| | 2273 | INPUT: |
| | 2274 | |
| | 2275 | - ``weights`` - Either individual numbers, a tuple or a dictionary, |
| | 2276 | specifying the weights of each variable. If it is a dictionary, it |
| | 2277 | maps each variable of ``self'' to its weight. If it is individual |
| | 2278 | numbers or a tuple, it specified the weights in the order of the |
| | 2279 | generators as given by ``self.parent().gens()'': |
| | 2280 | |
| | 2281 | EXAMPLES:: |
| | 2282 | |
| | 2283 | sage: R.<x,y,z> = GF(7)[] |
| | 2284 | sage: p = x^3 + y + x*z^2 |
| | 2285 | sage: p.weighted_degree({z:0, x:1, y:2}) |
| | 2286 | 3 |
| | 2287 | sage: p.weighted_degree(1, 2, 0) |
| | 2288 | 3 |
| | 2289 | sage: p.weighted_degree((1, 4, 2)) |
| | 2290 | 5 |
| | 2291 | sage: p.weighted_degree((1, 4, 1)) |
| | 2292 | 4 |
| | 2293 | sage: q = R.random_element(100, 20) #random |
| | 2294 | sage: q.weighted_degree(1,1,1) == q.total_degree() |
| | 2295 | True |
| | 2296 | """ |
| | 2297 | # Make the weights into a vector so we can use dot_product |
| | 2298 | if len(weights) == 1: |
| | 2299 | # First unwrap it if it is given as one element argument |
| | 2300 | weights = weights[0] |
| | 2301 | |
| | 2302 | if isinstance(weights, tuple): |
| | 2303 | weights = vector(weights) |
| | 2304 | elif isinstance(weights, dict): |
| | 2305 | weights_list = [ weights[g] for g in self.parent().gens() ] |
| | 2306 | weights = vector(weights_list) |
| | 2307 | else: |
| | 2308 | raise Exception('weights must be a tuple or a dictionary') |
| | 2309 | |
| | 2310 | # Go through each monomial, calculating the weight by dot product |
| | 2311 | deg = max([ weights.dot_product(vector(m.degrees())) |
| | 2312 | for m in self.monomials() ]) |
| | 2313 | return deg |
| | 2314 | |
| | 2315 | |