def normalizeData(data, meanOnly = False): """ normalize data by subtracting mean and dividing by sd per COLUMN @param data: an array @param meanOnly: if True subtract mean only; otherwise divide by sd too @return: (an array with the same dimension as data, mean, stds, the transformer) """ # compute the new data m = mean(data, axis=0) res = data - m if meanOnly: stds = 1 else: stds = sqrt(var(data, axis=0)) stds[stds==0] = 1 # to avoid dividing by 0 res /= stds # figure out the transformer def foo(givenData): assert givenData.shape[1]==data.shape[1], "Only arrays of %d columns are handled." % data.shape[1] return (givenData - m)/stds return res, m, stds, foo
def symMatrix(numRows, numCols, letter): A = matrix(SR, numRows, numCols) for i in range(0, numRows): for j in range(0, numCols): A[i, j] = var("%s%d%d" % (letter, i, j)) return A
def symVector(numRows, letter): """Creates a column vector where each element is a unique symbol """ n = len(str(numRows)) format = '%s%0' + str(n) + 'd' A = matrix(SR, numRows, 1) for i in range(0, numRows): A[i, 0] = var(format % (letter, i)) return A
def symMatrix( numRows , numCols , letter ): """Creates a matrix where each element is a unique symbol """ A = matrix(SR,numRows,numCols) for i in range(0,numRows): for j in range(0,numCols): A[i,j] = var('%s%d%d' % (letter,i,j) ) return A
def symVector( numRows , letter ): """Creates a column vector where each element is a unique symbol """ n = len(str(numRows)) format = '%s%0'+str(n)+'d' A = matrix(SR,numRows,1) for i in range(0,numRows): A[i,0] = var(format % (letter,i) ) return A
def symMatrix(numRows, numCols, letter): """Creates a matrix where each element is a unique symbol """ n = max(len(str(numRows)), len(str(numCols))) format = '%s%0' + str(n) + 'd' + '%0' + str(n) + 'd' A = matrix(SR, numRows, numCols) for i in range(0, numRows): for j in range(0, numCols): A[i, j] = var(format % (letter, i, j)) return A
def evolve(p, m): for j in range(200): for i in range(len(p.chromos)): sums = computeSums(m, p.chromos[i].genes[0], p.chromos[i].genes[1]) if len(sums) == 1: p.chromos[i].fitness = 0.1 else: # if var(sums) == 0.0: # print('Solution:') # print(sums) # return p.chromos[i].fitness = (1/(var(sums) + 0.0001)) #+ (len(sums)/(numR*numC)) if j%10 == 0: best = p.getBestIndividuals() sums = computeSums(m, best[0].genes[0], best[0].genes[1]) print("best") print(sums) print(var(sums)) print(best[0].fitness) p.newGeneration()
def symMatrix( numRows , numCols , letter ): """Creates a matrix where each element is a unique symbol """ n = max(len(str(numRows)),len(str(numCols))) format = '%s%0'+str(n)+'d'+'%0'+str(n)+'d' A = matrix(SR,numRows,numCols) for i in range(0,numRows): for j in range(0,numCols): A[i,j] = var(format % (letter,i,j) ) return A
def normalizeData(data, meanOnly = False): """ normalize data by subtracting mean and dividing by sd per COLUMN Parameters: - data: an array - meanOnly: if True subtract mean only; otherwise divide by sd too Returns: an array with the same dimension as data """ if meanOnly: return data-mean(data,axis=0) else: stds = sqrt(var(data, axis=0)) stds[stds==0] = 1 # to avoid dividing by 0 return (data - mean(data, axis=0))/stds
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Expands out symbolic equations for solving the linear system and polynomials in Nister's paper # The script should be run using sage by typing "sage nister5.py" # # Works with Sage Version 5.2 from numpy.core.fromnumeric import var from numpy.linalg.linalg import det from sage.all import * from utilsymbolic import * x,y,z = var('x','y','z') X = symMatrix( 3, 3 , 'X') Y = symMatrix( 3, 3 , 'Y') Z = symMatrix( 3, 3 , 'Z') W = symMatrix( 3, 3 , 'W') E = x*X + y*Y + z*Z + 1*W EE = E*E.T eq1=det(E) eq2=EE*E-0.5*EE.trace()*E eqs = (eq1,eq2[0,0],eq2[0,1],eq2[0,2],eq2[1,0],eq2[1,1],eq2[1,2],eq2[2,0],eq2[2,1],eq2[2,2]) keysA = ('x^3','y^3','x^2*y','x*y^2','x^2*z','x^2','y^2*z','y^2','x*y*z','x*y')
include = [removeFirst(w, bestVar) for w in include] output = bestVar + "*( " if bestVar[0].isdigit(): output += simplifyExpanded(reconstruct(include)) else: output += reconstruct(include) output += " )" if len(exclude): output += " + " + simplifyExpanded(reconstruct(exclude)) return output x, y, z = var("x", "y", "z") X = symMatrix(3, 3, "X") Y = symMatrix(3, 3, "Y") Z = symMatrix(3, 3, "Z") W = symMatrix(3, 3, "W") E = x * X + y * Y + z * Z + 1 * W EE = E * E.T eq1 = det(E) eq2 = EE * E - 0.5 * EE.trace() * E eqs = (eq1, eq2[0, 0], eq2[0, 1], eq2[0, 2], eq2[1, 0], eq2[1, 1], eq2[1, 2], eq2[2, 0], eq2[2, 1], eq2[2, 2]) keysA = ("x^3", "y^3", "x^2*y", "x*y^2", "x^2*z", "x^2", "y^2*z", "y^2", "x*y*z", "x*y")