({{, SWAP[x,y] 
swaps contents of the vectors x and y.}})

OpenersNotesSection(Spacer1)Details

To use SWAP, you first need to load the BLAS Package using (RefLink[Needs, paclet:ref/Needs])["LinearAlgebra`BLAS`"].

The following arguments must be given:

SWAP[x,y] is equivalent to {x,y}={y,x}.

The vector arguments must be of the same length.

OpenersPrimaryExamplesSection(Spacer1)Examples  (11)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Load the BLAS package:

Needs["LinearAlgebra`BLAS`"]

Swap two vectors :

x={1,2,3}; 
y={-1,0,1};
SWAP[x,y];
{x,y}

OpenersExampleSection1(Spacer1)Scope  (4)

Real vectors:

x={1.2,-3.1,0.4};
y={-2.9,1.6,2.2};
SWAP[x,y];
{x,y}

Complex vectors:

x={2.I,-3.+1.2I,3.};
y={0.4+7.3I,2.2,2.1I};
SWAP[x,y];
{x,y}

Arbitrary-precision vectors:

x={1`20,2,3}; 
y={-1`20,0,1};
SWAP[x,y];
{x,y}

Symbolic vectors:

x={v1,v2,v3};
y={w1,w2,w3};
SWAP[x,y];
{x,y}

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (2)

Swap the first two rows of a matrix in place:

m=Array[a,{3,3}];
SWAP[m[[1]],m[[2]]];
m//MatrixForm

Swap the second and third columns of a matrix in place:

m=Array[a,{3,3}];
SWAP[m[[All,2]],m[[All,3]]];
m//MatrixForm

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

SWAP[x,y] is equivalent to {x,y}={y,x}:

x=x1=RandomReal[1,{5}];
y=y1=RandomReal[1,{5}];
SWAP[x,y];
{x,y}=={y1,x1}

OpenersExampleSection5(Spacer1)Possible Issues  (3)

The arguments must be symbols:

SWAP[{1,2},{3,4}]

The arguments must be initialized to vectors:

Clear[x,y];
SWAP[x,y]

The arguments must be of equal length:

x={1,2};
y={3,4,5};
SWAP[x,y]

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Basic Linear Algebra Subroutines, paclet:LowLevelLinearAlgebra/guide/BLASGuide])}})}})