Skip to content

Commit

Permalink
Removed duplicate primes matrix in maths module
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Aug 16, 2017
1 parent e2ed45b commit b713991
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## 4.6.2
### Removed
- duplicate primes matrix in maths module

## 4.6.1
### Added
- tests utils
Expand Down
58 changes: 20 additions & 38 deletions hal/maths/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@

import random

LOW_PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271,
277, 281, 283, 293, 307, 311, 313, 317,
331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443,
449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, 547, 557, 563, 569, 571, 577,
587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647,
653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787,
797, 809, 811, 821, 823, 827, 829, 839,
853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929,
937, 941, 947, 953, 967, 971, 977, 983,
991, 997]


class Integer(object):
""" Big int std python won't recognize """

LOW_PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271,
277, 281, 283, 293, 307, 311, 313, 317,
331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443,
449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, 547, 557, 563, 569, 571, 577,
587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647,
653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787,
797, 809, 811, 821, 823, 827, 829, 839,
853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929,
937, 941, 947, 953, 967, 971, 977, 983,
991, 997]

def __init__(self, string):
self.to_int = int(string)
self.to_string = string
Expand All @@ -60,29 +60,11 @@ def is_probably_prime(self):
elif self.to_int % 2 == 0:
return False

low_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157,
163, 167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269,
271, 277, 281, 283, 293, 307, 311, 313, 317,
331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389,
397, 401, 409, 419, 421, 431, 433, 439, 443,
449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571, 577,
587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643,
647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773,
787, 797, 809, 811, 821, 823, 827, 829, 839,
853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919,
929, 937, 941, 947, 953, 967, 971, 977, 983,
991, 997]

if self.to_int in low_primes:
if self.to_int in LOW_PRIMES:
return True

# check if multiple pf low primes
for prime in low_primes:
for prime in LOW_PRIMES:
if self.to_int % prime == 0:
return False

Expand Down
60 changes: 30 additions & 30 deletions hal/ml/utils/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@


def precision(matrix):
""" Calculates accuracy on database
:param matrix: 2x2 matrix that looks like
True Positive - False Negative
| - |
False Positive - True Negative
"""
:param matrix: 2 x 2 matrix
True positives are in [0,0], true negatives are in [1,1],
false negatives are in [0,1] and false positives are in [1,0]
:return: float
Calculates accuracy on database
"""

true_pos = matrix[0][0]
Expand All @@ -40,12 +40,12 @@ def precision(matrix):


def recall(matrix):
""" Calculates recall on database
:param matrix: 2x2 matrix that looks like
True Positive - False Negative
| - |
False Positive - True Negative
"""
:param matrix: 2 x 2 matrix
True positives are in [0,0], true negatives are in [1,1],
false negatives are in [0,1] and false positives are in [1,0]
:return: float
Calculates recall on database
"""

true_pos = matrix[0][0]
Expand All @@ -58,12 +58,12 @@ def recall(matrix):


def true_neg_rate(matrix):
""" Calculates true negative rate on database
:param matrix: 2x2 matrix that looks like
True Positive - False Negative
| - |
False Positive - True Negative
"""
:param matrix: 2 x 2 matrix
True positives are in [0,0], true negatives are in [1,1],
false negatives are in [0,1] and false positives are in [1,0]
:return: float
Calculates true negative rate on database
"""

false_pos = matrix[1][0]
Expand All @@ -76,12 +76,12 @@ def true_neg_rate(matrix):


def accuracy(matrix):
""" Calculates recall on database
:param matrix: 2x2 matrix that looks like
True Positive - False Negative
| - |
False Positive - True Negative
"""
:param matrix: 2 x 2 matrix
True positives are in [0,0], true negatives are in [1,1],
false negatives are in [0,1] and false positives are in [1,0]
:return: float
Calculates accuracy on database
"""

true_pos = matrix[0][0]
Expand All @@ -98,12 +98,12 @@ def accuracy(matrix):


def f1_score(matrix):
""" Calculates f1 score on database
:param matrix: 2x2 matrix that looks like
True Positive - False Negative
| - |
False Positive - True Negative
"""
:param matrix: 2 x 2 matrix
True positives are in [0,0], true negatives are in [1,1],
false negatives are in [0,1] and false positives are in [1,0]
:return: float
Calculates F1 score on database
"""

m_pre = precision(matrix)
Expand Down

0 comments on commit b713991

Please sign in to comment.