ConstantArray[c,n]
generates a list of n copies of the element c.
ConstantArray[c,{n1,n2,…}]
generates an
array of nested lists containing copies of the element c.
ConstantArray
ConstantArray[c,n]
generates a list of n copies of the element c.
ConstantArray[c,{n1,n2,…}]
generates an
array of nested lists containing copies of the element c.
Details
- ConstantArray[c,n,SparseArray] gives the constant array as a SparseArray object.
Examples
open all close allBasic Examples (3)
Scope (4)
ConstantArray[0., 10]ConstantArray[0, 10]zm = ConstantArray[0., {3, 3}]ConstantArray[0, {3, 3}]ConstantArray[1, 10]A machine-number vector of ones:
ConstantArray[1., 10]A deeply nested constant array:
ConstantArray[x, {2, 1, 2, 1, 2}]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 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]]Properties & Relations (3)
ConstantArray[c,dims] has Dimensions[dims]:
dims = RandomInteger[{1, 4}, 5]Dimensions[ConstantArray[c, dims]]ConstantArray[c,dims] is equal to SparseArray[{},dims,c]:
dims = RandomInteger[{1, 4}, 5];s = SparseArray[{}, dims, c]ConstantArray[c, dims] == sNormal[s] is identical to ConstantArray[c,dims]:
ConstantArray[c, dims] === Normal[s]ConstantArray[c,dims] is equivalent to Apply[Table[c,##]&,Map[List,dims]]:
dims = RandomInteger[{1, 4}, 5];ConstantArray[c, dims] === Apply[Table[c, ##]&, Map[List, dims]]See Also
Array Table Range SparseArray IdentityMatrix UnitVector ConstantImage ArrayFlatten StringRepeat SymbolicZerosArray SymbolicOnesArray
Function Repository: ConstantAssociation CopyMachine
Text
Wolfram Research (2007), ConstantArray, Wolfram Language function, https://reference.wolfram.com/language/ref/ConstantArray.html (updated 2008).
CMS
Wolfram Language. 2007. "ConstantArray." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2008. https://reference.wolfram.com/language/ref/ConstantArray.html.
APA
Wolfram Language. (2007). ConstantArray. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ConstantArray.html
BibTeX
@misc{reference.wolfram_2026_constantarray, author="Wolfram Research", title="{ConstantArray}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/ConstantArray.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_constantarray, organization={Wolfram Research}, title={ConstantArray}, year={2008}, url={https://reference.wolfram.com/language/ref/ConstantArray.html}, note=[Accessed: 12-June-2026]}