Ejemplo n.º 1
0
def graph3(mode):
    local_results = defaultdict(list)
    for method in methods:
        for filter in ["1x1", "3x3", "5x5", "9x9"]:
            nthread = 8
            chunk_size = nthread
            key = (filter, method, nthread, chunk_size, default_file)
            run_perf(*key)
            local_results[method] += [float(results[key][mode])]

    title = (
        '4M pixels square image, #thread = 8, chunk_size = 8. Average over 10 runs.'
    )
    ylabel = mode
    if mode == 'time':
        ylabel += "(s)"

    #sets are unordered by default, so impose an order with this list.
    methods_as_list = list(methods.keys())
    filters_as_list = ["1x1", "3x3", "5x5", "9x9"]

    plotter.graph(
        filters_as_list,  # x axis
        [local_results[method] for method in methods_as_list],  # y vals
        methods_as_list,  # names for each line
        [colours[method] for method in methods_as_list],
        'graph_{}.png'.format(mode + "3"),  # filename
        title,
        "Filter Sizes",  # xlabel
        ylabel)
Ejemplo n.º 2
0
def graph2(mode, filter="3x3"):
    local_results = defaultdict(list)
    for nthread in threads:
        for chunk_size in chunk_sizes:
            method = "work queue"
            key = (filter, method, nthread, chunk_size, default_file)
            run_perf(*key)
            local_results[nthread] += [float(results[key][mode])]

    title = (
        '4M pixels square image, filter = {}, method = work queue. Average over 10 runs.'
        .format(filter))
    ylabel = mode
    if mode == 'time':
        ylabel += "(s)"

    #sets are unordered by default, so impose an order with this list.
    methods_as_list = list(methods.keys())

    plotter.graph(
        chunk_sizes,  # x axis
        [local_results[nthread] for nthread in threads],  # y vals
        threads,  # names for each line
        [colours[nthread] for nthread in threads],
        'graph_{}.png'.format(mode + filter + "2"),  # filename
        title,
        "Chuck Size",  # xlabel
        ylabel)
def graphnewimage(mode):
  local_results = defaultdict(list)
  for method in methods:
    for filter in filters:
      chunk_size = 8
      key = (filter, method, 8, chunk_size, default_file)
      run_perf(*key)
      local_results[method] += [float(results[key][mode])]

  title = ('100M pixels {} image, chunk_size (workqueue) = #'
      'threads. Average over 10 runs.'.format(default_file))
  ylabel = mode
  if mode == 'time':
    ylabel += "(s)"

  #sets are unordered by default, so impose an order with this list.
  methods_as_list = list(methods.keys())
  plotter.graph(
    [1,3,5,9], # x axis
    [local_results[method] for method in methods_as_list], # y vals
    methods_as_list,  # names for each line
    [colours[method] for method in methods_as_list],
    'graph_{}.png'.format(default_file), # filename
    title,
    "filters (N x N)", # xlabel
    ylabel
  )
def graph(mode, filter):
  local_results = defaultdict(list)
  for method in methods:
    for nthread in threads:
      chunk_size = nthread
      key = (filter, method, nthread, chunk_size, default_file)
      run_perf(*key)
      local_results[method] += [float(results[key][mode])]

  title = ('4M pixels square image, filter = {}, chunk_size (workqueue) = #'
      'threads. Average over 10 runs.'.format(filter))
  ylabel = mode
  if mode == 'time':
    ylabel += "(s)"

  #sets are unordered by default, so impose an order with this list.
  methods_as_list = list(methods.keys())
  plotter.graph(
    threads, # x axis
    [local_results[method] for method in methods_as_list], # y vals
    methods_as_list,  # names for each line
    [colours[method] for method in methods_as_list],
    'graph_{}.png'.format(mode+filter), # filename
    title,
    "# Threads", # xlabel
    ylabel
  )
def graphpool(mode, filter):
  local_results = defaultdict(list)
  for nthread in threads:
    for chunk_size in clunk_sizes:
      key = (filter, "work queue" , nthread, chunk_size, default_file)
      run_perf(*key)
      local_results[nthread] += [float(results[key][mode])]

  title = ('4M pixels square image, filter = {}, chunk_size (workqueue) = #'
      'threads. Average over 10 runs.'.format(filter))

  ylabel = mode
  ylabel += "(s)"

  plotter.graph(
    clunk_sizes, # x axis
    [local_results[nthread] for nthread in threads], # y vals
    ["1 thread","2 threads","3 threads","4 threads","5 threads","6 threads","7 threads","8 threads"],  # names for each line
    ["b","g","r","c","m","y","k","#fca308"],
    'graphpool_{}.png'.format(mode+filter), # filename
    title,
    "Chunk height", # xlabel
    ylabel
  )
Ejemplo n.º 6
0
def iodf(funct, x, tol, graf=1):
    '''
    |
    | Function that implements the improved Ostrowski’s method free from derivatives
    | to solve f(x) = 0
    |
    | ------------------------------------------------------------------------------
    | Parameters:
    | -----------
    |   funct :
    |        Text that represents the function f(x)
    |    x :
    |        Initial value of the iterative method
    |    tol :
    |        Stop criterion of the iterative method
    |    graf : 
    |        A number, 1 show the graph, 0 don't show the graph
    |        
    | Returns:
    | --------
    |    x_aprox :
    |       Approximation to the solution of the equation f(x) = 0
    |    iter :
    |        Number of iterations used to approximate the zero of the function
    |    graf : 
    |        Graph of iteration (k) vs errors (|f(xk)|) of the iterative method
    | ------------------------------------------------------------------------------
    |
    | The syntax rules for the input function are as follows:
    |     a. Use 'x' as variable name
    |     b. To multiply, add and subtract use '*', '+' and '-' respectively
    |     c. To place and exponent use '**'
    |     d. The function names of math library can be used (e.g., sqrt(), exp(), etc)
    |
    '''

    try:
        #Create a callable function from the input string function
        f = lambda x: eval(
            funct, {
                'x': x,
                'pi': pi,
                'e': e,
                'exp': exp,
                'log': log,
                'sqrt': sqrt,
                'cos': cos,
                'sin': sin,
                'tan': tan
            })

        #Iteration counter
        k = 0

        #Iterations array
        iterations = [k]
        #Function images f(x) array
        fxs = [abs(f(x))]

        while (abs(f(x)) >= tol):

            try:
                fx = f(x)
                y = x - (2 * (fx**2)) / (f(x + fx) - f(x - fx))

                fy = f(y)
                z = y - fy * ((y - x) / (2 * fy - fx))

                #Compute the current value of 'x'
                x = z - f(z) * ((y - x) / (2 * fy - fx))

                #Increase the iteration counter
                k += 1

                #Save the iteration values
                iterations.append(k)
                fxs.append(abs(f(x)))

            except ZeroDivisionError:
                #Stop the iterations if a zero division occur
                break

            print('-----------------------------------------------')
            print('Iteration ', k)
            print('y=', y)
            print('z=', z)
            print('x=', x)
            print('f(x)=', f(x))

        if (1 == graf):
            #Show 'iteration vs |f(x)|' graphic
            plt.graph(iterations, fxs)
        elif (0 != graf):
            #Shown a warning message if graf has an other value than 1 or 0
            print(
                'WARNING: El parámetro para mostrar la gráfica tiene un valor incorrecto!'
            )

        return {'x_aprox': x, 'iter': k}

    except (NameError, SyntaxError):

        print('ERROR: La función ingresada tiene una sintaxis incorrecta!')