The SEP article on Arabic and Islamic philosophy of mathematics discusses Avicenna's response to one version of mathematical realism:
[Mathematical objects] can be defined (or conceived) with no reference to matter or material beings. On the other hand, everything that is separate in definition (or in mind) is separate in existence. Therefore, the argument concludes, mathematical objects are separate in existence. They exist as fully separate beings which have no association with matter or material beings (Avicenna [MPh]: chap. VII.2, sec. 5). However, Avicenna finds this argument wanting. He argues that there is a difference between (a) defining (or conceiving) something without the condition of materiality and (b) defining (or conceiving) something with the condition of immateriality. He says that mathematical objects are separate in definition only in the sense of (a). But the second premise of the argument under discussion is true only if the separation in definition is considered in the sense of (b). The mere fact that something can be defined without the condition of materiality does not entail that that thing can exist in the extramental realm fully separate from matter. But mathematical objects cannot be defined with the condition of immateriality. It is not plausible to suppose immateriality as an essential component of the definitions of mathematical objects, or so Avicenna claims.
He apparently ends up with an empiricism-minded realism, however:
Most Muslim thinkers who have talked about the epistemology of mathematical concepts believe that these concepts are formed through some cognitive mechanisms whose first input is the data we receive through our external senses. The details of such mechanisms are spelled out in different ways by different philosophers, depending on their general picture of human cognitive psychology. For instance, Avicenna puts forward a thought experiment showing that no mathematical concepts can be grasped in the absence of sense perception (Avicenna [MPh], chap. VII.3, sec. 1; Zarepour 2019: sec. 5; 2021, sec. 3). This indicates that Avicenna endorses some sort of concept empiricism about mathematics.
Anderson discusses the "freedom of the mathematician" as a question for Aristotle and Thomas Aquinas:
[Quoting Aquinas] "The mathematician and the natural philosopher treat the same things, i. e., points, and lines, and surfaces, and things of this sort,
but not ill the same way. For the mathematician does not treat
these things insofar as each of them is a boundary of a natural
body, nor does he consider those things which belong to them insofar as they are the boundaries of a natural body. But this is the way in which natural science treats them .... Because the mathematician does not consider lines and points, and surfaces, and things of this sort, quantities and their accidents, insofar as they are the boundaries of a natural body, he is said to abstract from sensible and natural matter.
Anderson partly summarizes his interpretation of Aquinas thus:
... the imagination has an especially important role in mathematics for Aquinas-a role which, as we have said, is never mentioned by the Stagirite. For, in addition to providing a stable image from which the universal can be abstracted (this it does in all abstraction) in mathematics it furnishes to the intellect perfectly appropriate individual mathematicals, which simply cannot be found in nature, individuals from which the mathematical essence can then be abstracted. The direct senses are able to supply an appropriate object for the abstraction of physical essences; for the intellect's abstraction the imagination simply provides a stability in the changing objects grasped by sense. But the direct senses themselves cannot provide a perfectly appropriate object for abstraction of mathematical essences, for mathematical objects as such are not attainable by these senses. Rather the imagination, through its abstraction discussed above, provides the proper object, the suitable individual mathematical quantity, from which the mathematical essence can be abstracted.
Byl[16?] starts out by asking:
What does mathematics have to do with God? Not much, according to most people outside of this association. It is, therefore, quite surprising how often non-theists, when discussing the foundations of mathematics, refer to God. Many contemporary philosophers of science perceive theism to be the basis for classical mathematics and mathematical realism, both of which are therefore found to be objectionable.
I don't know who Byl is referring to, exactly, though Georg Cantor and Kurt Gödel were (more or less) mathematical realists who put God, as an actus purus or ens realissimum, at the apex of their worldview (or their general hopes, anyway). (Mueckenheim includes a quote from Cantor (page 24) in which the phrase "actus purus" is used; c.f. Cantor's thematic debt to Augustine (see Drozdek). For a neo-Anselmian thesis, see Steinhart.)
Even L. E. J. Brouwer, who was not an ante rem realist, flirted with a notion that echoes talk of mathematical gods or at least demigods (so to speak), the notion of a "freely creating mathematical subject" whose choice sequences piecemeal determine the constitution of a continuum a priori. A brief synopsis of Brouwer's life in the SEP doesn't emphasize any religious inclinations he might have had (if any), however.
I would tentatively suggest, then, that theism is weighted towards neither the discovery picture nor the invention picture, but might be better styled an intermediary or outlier standpoint with respect to the given question. We human mathematicians might "discover" the thoughts of God, on the above views, but conceptually, God's relationship to Its own thoughts would be less happenstance-based and more proactive, i.e. inventive. For example, if even arithmetic is "synthetic a priori" at least in the way of e.g., "1221 = 46005119909369701466112," being deniable without "contradiction," then one wonders whether a god of numbers would have the power to decide the otherwise contingent laws of arithmetic: to invent arithmetic, in other words.