({{, (RefLink[ConstantArray, paclet:ref/ConstantArray])[c,n] 
generates a list of n copies of the element c.}, {, (RefLink[ConstantArray, paclet:ref/ConstantArray])[c,{(Subscript[n, 1]),(Subscript[n, 2]),…}]
generates an (Subscript[n, 1])×(Subscript[n, 2])×… array of nested lists containing copies of the element c.}})

OpenersNotesSection(Spacer1)Details

(RefLink[ConstantArray, paclet:ref/ConstantArray])[c,n,(RefLink[SparseArray, paclet:ref/SparseArray])] gives the constant array as a (RefLink[SparseArray, paclet:ref/SparseArray]) object.

OpenersPrimaryExamplesSection(Spacer1)Examples  (13)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Make a vector of 10 c's:

ConstantArray[c,10]

Make a 3×4 array of c's:

ConstantArray[c,{3,4}]

Make an array out of blocks:

ArrayFlatten[ConstantArray[{{1,2},{3,4}},{3,3}]]

OpenersExampleSection1(Spacer1)Scope  (4)

A machine-zero vector:

ConstantArray[0.,10]

A vector of exact zeros:

ConstantArray[0,10]

A machine-zero matrix:

zm = ConstantArray[0.,{3,3}]

A matrix of exact zeros:

ConstantArray[0,{3,3}]

A vector of ones:

ConstantArray[1,10]

A machine-number vector of ones:

ConstantArray[1.,10]

A deeply nested constant array:

ConstantArray[x,{2,1,2,1,2}]

OpenersExampleSection3(Spacer1)Applications  (3)

Set up an array to use as storage in a program:

esteps[f_,x0_,h_,n_]:=Module[{res=ConstantArray[x0,{n+1}],x=x0},
Do[x+=h f[i h,x];res[[i+1]]=x,{i,n}];res]

Numerically solve x'[t]==1/(t+x[t]^2) using Euler's method:

sol = esteps[Function[{t,x},1/(t+x^2)],1,.01,100];

ListPlot[sol,DataRange->{0,1}]

Find the sum of the elements in a list:

list=RandomInteger[9,20]

list.ConstantArray[1,Length[list]]

This can also be done with (RefLink[Total, paclet:ref/Total]):

Total[list]

Use a vector of ones to see the effects of conditioning on the solution of a linear system:

ones = ConstantArray[1,10]

m=HilbertMatrix[10];
b=m.ones;

The solution at machine precision has significant errors:

x=LinearSolve[N[m],b]

Norm[x-ones]

At higher precision, the solution is much better:

x30=LinearSolve[N[m,30],b];
Norm[Block[{$MaxPrecision=30,$MinPrecision=30},x30-ones]]

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

(RefLink[ConstantArray, paclet:ref/ConstantArray])[c,dims] has (RefLink[Dimensions, paclet:ref/Dimensions])[dims]:

dims = RandomInteger[{1,4},5]

Dimensions[ConstantArray[c,dims]]

(RefLink[ConstantArray, paclet:ref/ConstantArray])[c,dims] is equal to (RefLink[SparseArray, paclet:ref/SparseArray])[{},dims,c]:

dims = RandomInteger[{1,4},5];

s = SparseArray[{},dims,c]

ConstantArray[c,dims] == s

(RefLink[Normal, paclet:ref/Normal])[s] is identical to (RefLink[ConstantArray, paclet:ref/ConstantArray])[c,dims]:

ConstantArray[c,dims]===Normal[s]

(RefLink[ConstantArray, paclet:ref/ConstantArray])[c,dims] is equivalent to (RefLink[Apply, paclet:ref/Apply])[(RefLink[Table, paclet:ref/Table])[c,##]&,(RefLink[Map, paclet:ref/Map])[(RefLink[List, paclet:ref/List]),dims]]:

dims=RandomInteger[{1,4},5];

ConstantArray[c,dims]===Apply[Table[c,##]&,Map[List,dims]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Array, paclet:ref/Array])  ▪  (RefLink[Table, paclet:ref/Table])  ▪  (RefLink[Range, paclet:ref/Range])  ▪  (RefLink[SparseArray, paclet:ref/SparseArray])  ▪  (RefLink[IdentityMatrix, paclet:ref/IdentityMatrix])  ▪  (RefLink[UnitVector, paclet:ref/UnitVector])  ▪  (RefLink[ConstantImage, paclet:ref/ConstantImage])  ▪  (RefLink[ArrayFlatten, paclet:ref/ArrayFlatten])  ▪  (RefLink[StringRepeat, paclet:ref/StringRepeat])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Constructing Lists, paclet:guide/ConstructingLists])}, {▪(RefLinkPlain[Operations on Vectors, paclet:guide/OperationsOnVectors])}, {▪(RefLinkPlain[Structure Matrices & Convolution Kernels, paclet:guide/StructureMatricesAndConvolutionKernels])}, {▪(RefLinkPlain[Constructing Matrices, paclet:guide/ConstructingMatrices])}})}})