def test1(): import numpy as np import pylab from scipy import sparse from regreg.algorithms import FISTA from regreg.atoms import l1norm from regreg.container import container from regreg.smooth import signal_approximator, smooth_function Y = np.random.standard_normal(500) Y[100:150] += 7 Y[250:300] += 14 sparsity = l1norm(500, l=1.0) # Create D D = (np.identity(500) + np.diag([-1] * 499, k=1))[:-1] D = sparse.csr_matrix(D) fused = l1norm(D, l=19.5) p = container(loss, sparsity, fused) soln1 = blockwise(pen, Y) solver = FISTA(p.problem()) solver.fit(max_its=800, tol=1e-10) soln2 = solver.problem.coefs # plot solution pylab.figure(num=1) pylab.clf() pylab.scatter(np.arange(Y.shape[0]), Y, c="r") pylab.plot(soln1, c="y", linewidth=6) pylab.plot(soln2, c="b", linewidth=2)
def test1(): import numpy as np import pylab from scipy import sparse from regreg.algorithms import FISTA from regreg.atoms import l1norm from regreg.container import container from regreg.smooth import quadratic Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14 sparsity = l1norm(500, lagrange=1.0) #Create D D = (np.identity(500) + np.diag([-1]*499,k=1))[:-1] D = sparse.csr_matrix(D) fused = l1norm.linear(D, lagrange=19.5) loss = quadratic.shift(-Y, lagrange=0.5) p = container(loss, sparsity, fused) soln1 = blockwise([sparsity, fused], Y) solver = FISTA(p) solver.fit(max_its=800,tol=1e-10) soln2 = solver.composite.coefs #plot solution pylab.figure(num=1) pylab.clf() pylab.scatter(np.arange(Y.shape[0]), Y, c='r') pylab.plot(soln1, c='y', linewidth=6) pylab.plot(soln2, c='b', linewidth=2)
def test1(): import numpy as np import pylab from scipy import sparse from regreg.algorithms import FISTA from regreg.atoms import l1norm from regreg.container import container from regreg.smooth import signal_approximator, smooth_function Y = np.random.standard_normal(500) Y[100:150] += 7 Y[250:300] += 14 sparsity = l1norm(500, l=1.0) #Create D D = (np.identity(500) + np.diag([-1] * 499, k=1))[:-1] D = sparse.csr_matrix(D) fused = l1norm(D, l=19.5) p = container(loss, sparsity, fused) soln1 = blockwise(pen, Y) solver = FISTA(p.problem()) solver.fit(max_its=800, tol=1e-10) soln2 = solver.problem.coefs #plot solution pylab.figure(num=1) pylab.clf() pylab.scatter(np.arange(Y.shape[0]), Y, c='r') pylab.plot(soln1, c='y', linewidth=6) pylab.plot(soln2, c='b', linewidth=2)
def test2(): import numpy as np import pylab from scipy import sparse from regreg.algorithms import FISTA from regreg.atoms import l1norm from regreg.container import container from regreg.smooth import signal_approximator, smooth_function n1, n2 = l1norm(1), l1norm(1) Y = np.array([30.0]) l = signal_approximator(Y) p = container(l, n1, n2) blockwise(s, Y, p.problem())
def test2(): import numpy as np import pylab from scipy import sparse from regreg.algorithms import FISTA from regreg.atoms import l1norm from regreg.container import container from regreg.smooth import signal_approximator, smooth_function n1, n2 = l1norm(1), l1norm(1) Y = np.array([30.]) l = signal_approximator(Y) p = container(l, n1, n2) blockwise(s, Y, p.problem())
from container import * try: ifst = open("input.txt").read().split("\n") except: exit() ofst = open("output.txt", "w") ofst_filtr = open("output1.txt", "w") print("Start.") c = container() InData(c.matrices, ifst) ofst.write("Filled container.\n") Sort(c.matrices) OutData(c.matrices, ofst) OutDataFiltr(c.matrices, ofst_filtr) Clear(c.matrices) ofst.write("Empty container.\n") OutData(c.matrices, ofst) print("Stop") ofst.close()
def banana_verify(source_word, goal_word, container, list_of_moves): '''(str, str, Container, list) -> bool REQ: len(source_word) > 0 REQ: len(goal_word) > 0 REQ: list_of_moves must have only 'M','G','P' REQ: len(list_of_moves) >= 0 >>> banana_verify("BANANA", "AAANNB", stack(), ["P","M","P","M","P","M","G","G","G"]) True >>> banana_verify("BANANA", "AAANNBATMAN", stack(), ["P", "G"]) False >>> banana_verify("BANANA","AAABBN", bucket(), ["M", "G"]) False >>> banana_verify("BANANA","NABANA", queue(), ['P','P','M','M','G','G','M','M']) True Returns True iff the word created after applying list_of_moves given is the same as goal_word. If the word created is not the same as the goal_word, then return False. ''' # this str will turn into a word, as moves are applies to it formed_word = str() gg = container() # if an exception error is raised, then return false try: # for each word in the list of moves for moves in list_of_moves: # if moves are equal to 'P' if moves == 'P': # store the first letter from the word, which is to be Put # into container stored_letter = source_word[0] # make the source word equal to everything except the # first letter source_word = source_word[1:] # put the letter into container gg.put(stored_letter) # if the move is 'M', then elif moves == 'M': # store first letter from source_word word_to_move = source_word[0] # Remove the first letter from source_word source_word = source_word[1:] # add the letter at the end of formed_word formed_word += word_to_move # if move is 'G', which is Get, then get next word from container elif moves == 'G': # get word from container and store it get_word = gg.get() # add that word to end of formed_word formed_word += get_word # if the formed_word is the same as goal_word, then return True if formed_word == goal_word: return True else: return False # if an error occurs, make sure code does not crash, instead returns False except: return False