4 added 113 characters in body
source | link

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current acceptedciao's answer here

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[LinearAlgebra`AddVectorToMatrixDiagonal[arg, -Diagonal[arg]];]

{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]

{1.41651, Null}

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[LinearAlgebra`AddVectorToMatrixDiagonal[arg, -Diagonal[arg]];]

{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]

{1.41651, Null}

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with ciao's answer here

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[LinearAlgebra`AddVectorToMatrixDiagonal[arg, -Diagonal[arg]];]

{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]

{1.41651, Null}

3 added 290 characters in body
source | link

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[LinearAlgebra`AddVectorToMatrixDiagonal[arg, -Diagonal[arg]];]

{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]

{1.41651, Null}

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

{0.406152, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[LinearAlgebra`AddVectorToMatrixDiagonal[arg, -Diagonal[arg]];]

{0.356579, Null}

arg = RandomReal[10, {10^4, 10^4}];
AbsoluteTiming[UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1];]

{1.41651, Null}

2 added 290 characters in body
source | link

Method one:LinearAlgebra`SetMatrixDiagonal

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Method one:LinearAlgebra`SetMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`SetMatrixDiagonal[mat,Array[0 &, Length[mat]]] // MatrixForm

Developer`PackedArrayQ[mat]

True

Method two:LinearAlgebra`AddVectorToMatrixDiagonal

mat = RandomReal[10, {3, 3}];
LinearAlgebra`AddVectorToMatrixDiagonal[mat, -Diagonal[mat]] // MatrixForm

Developer`PackedArrayQ[mat]

True

ps:Note this two method will change the original mat.

Compare with current accepted answer

arg = RandomReal[10, {10^4, 10^4}];
LinearAlgebra`SetMatrixDiagonal[arg,Array[0 &, Length[arg]]]; // AbsoluteTiming

But UpperTriangularize[arg, 1] + LowerTriangularize[arg, -1] will give up to calculate.

{0.406152, Null}

1
source | link