swap (Linear Algebra)


Import

#include <archr/algebra/swap.h>
#include <archr/algebra/swap.hpp>

Synopsis

 void archr_algebra_swap_f32(float *lhs, float *rhs, size_t nbelem);
(1)
 void archr_algebra_swap_f64(double *lhs, double *rhs, size_t nbelem);
(2)
 void archr_algebra_swap_zf32(void *lhs, void *rhs, size_t nbelem);
(3)
 void archr_algebra_swap_zf64(void *lhs, void *rhs, size_t nbelem);
(4)
 void archr_algebra_swapr_f32(float *lhs, float *first, float *last);
(5)
 void archr_algebra_swapr_f64(double *lhs, double *first, double *last);
(6)
 void archr_algebra_swapr_zf32(void *lhs, void *first, void *last);
(7)
 void archr_algebra_swapr_zf64(void *lhs, void *first, void *last);
(8)
 void swap(float *rhs, size_t nbelem, float *lhs);
(1)
 void swap(double *rhs, size_t nbelem, double *lhs);
(2)
 void swap(std::complex<float>*rhs, size_t nbelem, std::complex<float>*lhs);
(3)
 void swap(std::complex<double>*rhs, size_t nbelem, std::complex<double>*lhs);
(4)
 void swap(float *first, float *last, float *lhs);
(5)
 void swap(double *first, double *last, double *lhs);
(6)
 void swap(std::complex<float>*first, std::complex<float>*last, std::complex<float>*lhs);
(7)
 void swap(std::complex<double>*first, std::complex<double>*last, std::complex<double>*lhs);
(8)
template <typename Range> void swap(Range& rhs, Range& lhs);
(9)
subroutine archr_algebra_swap_f32(real(4), dimension(*) :: rhs, integer(4) :: nbelem, real(4), dimension(*) :: lhs)
(1)
subroutine archr_algebra_swap_f64(real(8), dimension(*) :: rhs, integer(4) :: nbelem, real(8), dimension(*) :: lhs)
(2)
subroutine archr_algebra_swap_zf32(complex(4), dimension(*) :: rhs, integer(4) :: nbelem, complex(4), dimension(*) :: lhs)
(3)
subroutine archr_algebra_swap_zf64(complex(8), dimension(*) :: rhs, integer(4) :: nbelem, complex(8), dimension(*) :: lhs)
(4)

Description

This function swaps every element of the input ranges stored in:

Parameters

lhs, first

the beginning of the first range.

last

the end of the first range.

rhs

the beginning of the second range.

nbelem

the number of elements to process.

Example

#include <archr/algebra/swap.h>
#include <stdio.h>

int main()
{
  float v[4] = {1,3,5,7};
  float w[4] = {2,4,6,8};

  printf("v {%f %f %f %f}\n", v[0], v[1], v[2], v[3]);
  printf("w {%f %f %f %f}\n\n", w[0], w[1], w[2], w[3]);

  archr_algebra_swap_f32(&v[0], &w[0], 4);

  printf("v {%f %f %f %f}\n", v[0], v[1], v[2], v[3]);
  printf("w {%f %f %f %f}\n", w[0], w[1], w[2], w[3]);

  return 0;
}

Possible Output

v {1.000000 3.000000 5.000000 7.000000}
w {2.000000 4.000000 6.000000 8.000000}

v {2.000000 4.000000 6.000000 8.000000}
w {1.000000 3.000000 5.000000 7.000000}
#include <archr/algebra/swap.hpp>
#include <iostream>
#include <vector>

void print(std::vector<float> const& v)
{
  std::cout << "{ ";
  for(auto e : v) std::cout << e << " ";
  std::cout << "}";
}

int main()
{
  std::vector<float> v{1,3,5,7}, w{2,4,6,8};

  std::cout << "v = ";
  print(v);
  std::cout << "\n";

  std::cout << "w = ";
  print(w);
  std::cout << "\n\n";

  archr::algebra::swap(v,w);

  std::cout << "v = ";
  print(v);
  std::cout << "\n";

  std::cout << "w = ";
  print(w);
  std::cout << "\n";

  return 0;
}

Possible Output

v = { 1 3 5 7 }
w = { 2 4 6 8 }

v = { 2 4 6 8 }
w = { 1 3 5 7 }
program swap
  integer(4), parameter     :: sz = 7
  real(8)   , dimension(sz) :: dstf64, srcf64

  do i=1,sz
    srcf64(i) =  i
    dstf64(i) = -i
  end do

  print *, dstf64
  print *, srcf64

  call archr_algebra_swap_f64(dstf64, srcf64, size(dstf64))

  print *, dstf64
  print *, srcf64

end program

Possible Output

  -1.0000  -2.0000  -3.0000  -4.0000  -5.0000  -6.0000  -7.0000
   1.0000   2.0000   3.0000   4.0000   5.0000   6.0000   7.0000
   1.0000   2.0000   3.0000   4.0000   5.0000   6.0000   7.0000
  -1.0000  -2.0000  -3.0000  -4.0000  -5.0000  -6.0000  -7.0000