Skip to main content
More minor improvements, inspections in PyCharm reduced to 2
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
import numpy as np
import itertools as it
importfrom math import factorial
import re


def permutation(m, n):
    return math.factorial(n) / (math.factorial(n - m) * math.factorial(m))


def matrix_combinations(matr, n):
    timed = list(map(list, it.combinations(matr, n)))
    for i in range(n):
        timed[i][i][i] = np.asscalar(timed[i][i][i])
    return np.array(list(timed))


def array_combinations(arr, n):
    timed = list(map(list, it.combinations(arr, n)))
    for i in range(n):
        timed[i][i] = np.asscalar(timed[i][i])
    return np.array(list(timed))


def check_extreme(matr, arr, x, sym_comb, m):
    sym_comb = sym_comb.replace(']', '')
    sym_comb = sym_comb.replace('[', '')
    sym_comb = re.split("[ ,]", sym_comb)
    for i in range(int(m)):
        td_answer = sum(matr[i] * x)
        if sym_comb[i] == '>':
            if td_answer <= arr[i]:
                return 0
        elif sym_comb[i] == '>=':
            if td_answer < arr[i]:
                return 0
        elif sym_comb[i] == '<':
            if td_answer >= arr[i]:
                return 0
        elif sym_comb[i] == '<=':
            if td_answer > arr[i]:
                return 0
        elif sym_comb[i] == '=':
            if td_answer != arr[i]:
                return 0
        elif sym_comb[i] == '!=':
            if td_answer == arr[i]:
                return 0
        else:
            return 0
    return 1


def extreme_points(m, n, A, b, sym_comb):
    # Input
    A = np.array(A).reshape(m, n)
    b = np.array(b).reshape(
    m, 1)n = A.shape
    # Process
    ans_comb = np.zeros((1, n))
    arr_comb = array_combinations(b, n)
    matr_comb = matrix_combinations(A, n)
    for i in range(int(permutation(n, m))):
        if np.linalg.det(matr_comb[i]) != 0:
            x = np.linalg.solve(np.array(matr_comb[i], dtype='float'),
                                np.array(arr_comb[i], dtype='float'))
            ans_comb = np.vstack([ans_comb, x])
    ans_comb = np.delete(ans_comb, 0, axis=0)
    j = 0
    for i in range(len(ans_comb)):
        if check_extreme(A, b, ans_comb[j], sym_comb, m):
            ans_comb = ans_comb
            j = j ++= 1
        else:
            ans_comb = np.delete(ans_comb, j, axis=0)
    # Output
    return ans_comb

Notable changes:

  • Do a direct import of factorial
  • Don't call asscalar, since it's both unneeded and deprecated
  • Don't call a variable all, since that shadows a Python built-in
  • Don't need to explicitly pass array dimensions, nor do you need to reshape the arrays
  • Drop redundant parens around some expressions
  • Use += where applicable
  • Fix up almost all PEP8 issues, except for your capital letter A, which is fine in context

This is a first cut mainly for PEP8 compliance, and doesn't solve the bigger issue that you should replace 99% of this with a call to scipy. I'll do that separately (I suspect that @vnp is, as well).

import numpy as np
import itertools as it
import math
import re


def permutation(m, n):
    return math.factorial(n) / (math.factorial(n - m) * math.factorial(m))


def matrix_combinations(matr, n):
    timed = list(map(list, it.combinations(matr, n)))
    for i in range(n):
        timed[i][i][i] = np.asscalar(timed[i][i][i])
    return np.array(list(timed))


def array_combinations(arr, n):
    timed = list(map(list, it.combinations(arr, n)))
    for i in range(n):
        timed[i][i] = np.asscalar(timed[i][i])
    return np.array(list(timed))


def check_extreme(matr, arr, x, sym_comb, m):
    sym_comb = sym_comb.replace(']', '')
    sym_comb = sym_comb.replace('[', '')
    sym_comb = re.split("[ ,]", sym_comb)
    for i in range(m):
        td_answer = sum(matr[i] * x)
        if sym_comb[i] == '>':
            if td_answer <= arr[i]:
                return 0
        elif sym_comb[i] == '>=':
            if td_answer < arr[i]:
                return 0
        elif sym_comb[i] == '<':
            if td_answer >= arr[i]:
                return 0
        elif sym_comb[i] == '<=':
            if td_answer > arr[i]:
                return 0
        elif sym_comb[i] == '=':
            if td_answer != arr[i]:
                return 0
        elif sym_comb[i] == '!=':
            if td_answer == arr[i]:
                return 0
        else:
            return 0
    return 1


def extreme_points(m, n, A, b, sym_comb):
    # Input
    A = np.array(A).reshape(m, n)
    b = np.array(b).reshape(m, 1)
    # Process
    ans_comb = np.zeros((1, n))
    arr_comb = array_combinations(b, n)
    matr_comb = matrix_combinations(A, n)
    for i in range(int(permutation(n, m))):
        if np.linalg.det(matr_comb[i]) != 0:
            x = np.linalg.solve(matr_comb[i], arr_comb[i])
            ans_comb = np.vstack([ans_comb, x])
    ans_comb = np.delete(ans_comb, 0, axis=0)
    j = 0
    for i in range(len(ans_comb)):
        if check_extreme(A, b, ans_comb[j], sym_comb, m):
            ans_comb = ans_comb
            j = j + 1
        else:
            ans_comb = np.delete(ans_comb, j, axis=0)
    # Output
    return ans_comb

This is a first cut mainly for PEP8 compliance, and doesn't solve the bigger issue that you should replace 99% of this with a call to scipy. I'll do that separately (I suspect that @vnp is, as well).

import numpy as np
import itertools as it
from math import factorial
import re


def permutation(m, n):
    return factorial(n) / (factorial(n - m) * factorial(m))


def matrix_combinations(matr, n):
    timed = list(map(list, it.combinations(matr, n)))
    return np.array(list(timed))


def array_combinations(arr, n):
    timed = list(map(list, it.combinations(arr, n)))
    return np.array(list(timed))


def check_extreme(matr, arr, x, sym_comb, m):
    sym_comb = sym_comb.replace(']', '')
    sym_comb = sym_comb.replace('[', '')
    sym_comb = re.split("[ ,]", sym_comb)
    for i in range(int(m)):
        td_answer = sum(matr[i] * x)
        if sym_comb[i] == '>':
            if td_answer <= arr[i]:
                return 0
        elif sym_comb[i] == '>=':
            if td_answer < arr[i]:
                return 0
        elif sym_comb[i] == '<':
            if td_answer >= arr[i]:
                return 0
        elif sym_comb[i] == '<=':
            if td_answer > arr[i]:
                return 0
        elif sym_comb[i] == '=':
            if td_answer != arr[i]:
                return 0
        elif sym_comb[i] == '!=':
            if td_answer == arr[i]:
                return 0
        else:
            return 0
    return 1


def extreme_points(A, b, sym_comb):
    # Input
    A = np.array(A)
    b = np.array(b)
    m, n = A.shape
    # Process
    ans_comb = np.zeros((1, n))
    arr_comb = array_combinations(b, n)
    matr_comb = matrix_combinations(A, n)
    for i in range(int(permutation(n, m))):
        if np.linalg.det(matr_comb[i]) != 0:
            x = np.linalg.solve(np.array(matr_comb[i], dtype='float'),
                                np.array(arr_comb[i], dtype='float'))
            ans_comb = np.vstack([ans_comb, x])
    ans_comb = np.delete(ans_comb, 0, axis=0)
    j = 0
    for i in range(len(ans_comb)):
        if check_extreme(A, b, ans_comb[j], sym_comb, m):
            ans_comb = ans_comb
            j += 1
        else:
            ans_comb = np.delete(ans_comb, j, axis=0)
    # Output
    return ans_comb

Notable changes:

  • Do a direct import of factorial
  • Don't call asscalar, since it's both unneeded and deprecated
  • Don't call a variable all, since that shadows a Python built-in
  • Don't need to explicitly pass array dimensions, nor do you need to reshape the arrays
  • Drop redundant parens around some expressions
  • Use += where applicable
  • Fix up almost all PEP8 issues, except for your capital letter A, which is fine in context

This doesn't solve the bigger issue that you should replace 99% of this with a call to scipy. I'll do that separately (I suspect that @vnp is, as well).

Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

I created a rudimentary pull request to your GitHub repo. I won't show all of the content here except for the main file:

import numpy as np
import itertools as it
import math
import re


def permutation(m, n):
    return math.factorial(n) / (math.factorial(n - m) * math.factorial(m))


def matrix_combinations(matr, n):
    timed = list(map(list, it.combinations(matr, n)))
    for i in range(n):
        timed[i][i][i] = np.asscalar(timed[i][i][i])
    return np.array(list(timed))


def array_combinations(arr, n):
    timed = list(map(list, it.combinations(arr, n)))
    for i in range(n):
        timed[i][i] = np.asscalar(timed[i][i])
    return np.array(list(timed))


def check_extreme(matr, arr, x, sym_comb, m):
    sym_comb = sym_comb.replace(']', '')
    sym_comb = sym_comb.replace('[', '')
    sym_comb = re.split("[ ,]", sym_comb)
    for i in range(m):
        td_answer = sum(matr[i] * x)
        if sym_comb[i] == '>':
            if td_answer <= arr[i]:
                return 0
        elif sym_comb[i] == '>=':
            if td_answer < arr[i]:
                return 0
        elif sym_comb[i] == '<':
            if td_answer >= arr[i]:
                return 0
        elif sym_comb[i] == '<=':
            if td_answer > arr[i]:
                return 0
        elif sym_comb[i] == '=':
            if td_answer != arr[i]:
                return 0
        elif sym_comb[i] == '!=':
            if td_answer == arr[i]:
                return 0
        else:
            return 0
    return 1


def extreme_points(m, n, A, b, sym_comb):
    # Input
    A = np.array(A).reshape(m, n)
    b = np.array(b).reshape(m, 1)
    # Process
    ans_comb = np.zeros((1, n))
    arr_comb = array_combinations(b, n)
    matr_comb = matrix_combinations(A, n)
    for i in range(int(permutation(n, m))):
        if np.linalg.det(matr_comb[i]) != 0:
            x = np.linalg.solve(matr_comb[i], arr_comb[i])
            ans_comb = np.vstack([ans_comb, x])
    ans_comb = np.delete(ans_comb, 0, axis=0)
    j = 0
    for i in range(len(ans_comb)):
        if check_extreme(A, b, ans_comb[j], sym_comb, m):
            ans_comb = ans_comb
            j = j + 1
        else:
            ans_comb = np.delete(ans_comb, j, axis=0)
    # Output
    return ans_comb

This is a first cut mainly for PEP8 compliance, and doesn't solve the bigger issue that you should replace 99% of this with a call to scipy. I'll do that separately (I suspect that @vnp is, as well).