Question

In: Computer Science

Using c++, complete the functions to fulfill requirements. Basically, turn test case into unit test code....

Using c++, complete the functions to fulfill requirements. Basically, turn test case into unit test code.

You may only use:

- iostream, cmath

/*
BLAS Level 1 function declarations
*/

#ifndef BLAS_H
#define BLAS_H

/*
Find the element with the maximum absolute value.
Input:
   x : vector (as array of doubles)
   len : number of elements in x (dimension)
Output: index of the first element with maximum absolute value
Errors: returns -1 if vector is empty (len = 0)
*/
int amax(
const double* x,
const unsigned int len);

/*
Sum the absolute values.
Input:
   x : vector (as array of doubles)
   len : number of elements in x (dimension)
Output: sum of the absolute values of the elements.
Returns 0 if x is empty.
*/
double asum(
const double* x,
const unsigned int len);

/*
Generalized vector addition (afine transformation)
Input:
   a : scalar value
   x : vector (as an array of doubles)
   y : vector (as an array of doubles)
   len : number of elements in x and y (dimension, assumed equal)
Output: None. y is updated as y = a*x + y
*/
void axpy(
const double a,
const double* x,
double* y,
const unsigned int len);

/*
Copy a vector.
Input:
   x : source vector (as an array of doubles)
   y : destination vector (as array of doubles)
   len : number of elements in x and y (dimension, assumed equal)
Output: None. y is updated as y = x
*/
void copy(
const double* x,
double* y,
const unsigned int len);

/*
Dot (inner) product of two vectors.
Input:
   x : vector (as an array of doubles)
   y : vector (as an array of doubles)
   len : number of elements of x and y (dimension, assumed to be equal)
Output: dot (inner) product of x and y: x . y
Return 0 if x and y are empty.
*/
double dot(
const double* x,
const double* y,
const unsigned int len);

/*
Euclidean norm of a vector.
Input:
   x : vector (as an array of doubles)
   len : number of elements of x (dimension)
Output: magnitude (length) of x as the distance from the origin to x (as a point)
Returns 0 if x is empty.
*/
double norm2(
const double* x,
const unsigned int len);

/*
Scale a vector.
Input:
   a : scalar value
   x : vector (as an array of doubles)
   len : number of elements in x (dimension)
Output: None. x is scaled by a: x = a*x;
*/
void scale(
const double a,
double* x,
const unsigned int len);

/*
Swap two vectors.
Input:
   x : vector (as an array of doubles)
   y : vector (as an array of doubles)
   len : number of elements in x and y (dimension, assumed to be equal)
Output: None. x and y are updated as x,y = y,x
*/
void swap(
double* x,
double* y,
const unsigned int len);

#endif

Solutions

Expert Solution

//blas.h

#ifndef BLAS_H
#define BLAS_H

int amax(const double* x, const unsigned int len);

double asum(const double* x, const unsigned int len);

void axpy(const double a, const double* x, double* y, const unsigned int len);

void copy(const double* x, double* y, const unsigned int len);

double dot(const double* x, const double* y, const unsigned int len);

double norm2(const double* x,const unsigned int len);

void scale(const double a,double* x, const unsigned int len);

void swap(double* x, double* y, const unsigned int len);

#endif

//blas.cpp

#include "blas.h"
#include<math.h>

int amax(const double* x, const unsigned int len) {
   if(len==0)return -1;
   double max = abs(x[0]);
   unsigned int i;
   for(i=0;i<len;i++){
       if(abs(x[i])>max)max = abs(x[i]);
   }
return max;
}

double asum(const double* x, const unsigned int len) {
   double sum=0;
   unsigned int i;
   for(i=0;i<len;i++){
       sum+=abs(x[i]);
   }
return sum;
}

void axpy(const double a, const double* x, double* y, const unsigned int len) {
   unsigned int i;
for(i=0;i<len;i++){
   y[i] = a*x[i] + y[i];
   }
}

void copy(const double* src, double* dest, const unsigned int len) {
   unsigned int i;
for(i=0;i<len;i++){
   dest[i] = src[i];
   }
}

double dot(const double* x, const double* y, const unsigned int len) {
   double total=0;
   unsigned int i;
   for(i=0;i<len;i++){
       total+=x[i]*y[i];
   }
return total;
}

double norm2(const double* x, const unsigned int len) {
return sqrt(dot(x,x,len));
}

void scale(const double a, double* x, const unsigned int len) {
   unsigned int i;
for(i=0;i<len;i++){
   x[i] = x[i]*a;
   }
}

void swap(double* x, double* y, const unsigned int len) {
   double temp;
   unsigned int i;
for(i=0;i<len;i++){
   temp=x[i];
   x[i] = y[i];
   y[i] = temp;
   }
}


Related Solutions

For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Please Please fulfill the requirements and error handling and make this in basic C++ construct Thank...
Please Please fulfill the requirements and error handling and make this in basic C++ construct Thank you!! Objectives: Perform C++ string object manipulation Understand how to manipulate data using arrays Handle input errors and invalid values Design and create a well-structure program using C++ basic programming constructs Description: Write a menu-driven program that provides the following options: Show All Spend Search expenses containing this string Search expenses with greater than or equal to this amount Exit It allows the user...
Please complete the following functions using C. ------------------------------------------------------------ #include #include "dynarray.h" /* * This is the...
Please complete the following functions using C. ------------------------------------------------------------ #include #include "dynarray.h" /* * This is the definition of the dynamic array structure you'll use for your * implementation. Importantly, your dynamic array implementation will store * each data element as a void* value. This will permit data of any type to * be stored in your array. Because each individual element will be stored in * your array as type void*, the data array needs to be an array of...
Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them...
Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them from a simple interactive main() function using a menu, with different values used to select the choice of function. Overall, you should have one C program (call it Lab1.c) containing one main() function and 5 other functions, where the functions are called based on an interactive user menu. The program should contain a loop that permits users to enter a new choice of function...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
c++ code Reverse the order of elements in a vector of integers using two functions: (a)...
c++ code Reverse the order of elements in a vector of integers using two functions: (a) The first function takes the input vector a and return a vector b with the input data but in the reverse order, e.g. input: 1, 2, 3 and output 3, 2, 1. (b) The second function is a recursive one and it reverses the input data in place (without using an additional vector).
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT