1
$\begingroup$

I'm new to Mathematica and am trying to implement some simple noise functions in it in hopes of eventually finding an integral for them. This is the interesting part of the code:

PRNTable = Import["C:/Users/alemariusnexus/noisetest/prntable.csv"];
PermTable = Flatten[Import["C:/Users/alemariusnexus/noisetest/permtable.csv"]];

latticeidx[{x_}] := PermTable[[Mod[x, Length[PermTable]] + 1]];
lnoise[ip_] := PRNTable[[latticeidx[ip] + 1, 1]];

crBaseMatrix = Transpose[{
    {0, 1, 0, 0},
    {-0.5, 0, 0.5, 0},
    {1, -2.5, 2, -0.5},
    {-0.5, 1.5, -1.5, 0.5}
    }];
crspline[t_, cps_List] := Module[{n, globalT, baseIdx, localT}, 
   n = Length[cps];
   globalT = t*(n - 3);
   baseIdx = Clip[Floor[globalT], {0, n - 4}];
   localT = Clip[globalT - baseIdx, {0, 1}];
   Transpose[
     Table[cps[[
       baseIdx + x + 1]], {x, {0, 1, 2, 3}}]] . (crBaseMatrix . {1, 
       localT, localT*localT, localT*localT*localT})
   ];

vnoisebase[{x_Real}, {axis_List}, offset_List] := crspline[
   x - Floor[x],
   Table[
    {lnoise[(Floor[x] + w) * axis + offset]},
    {w, -1, 2}
    ]
   ];

Evaluate[Integrate[vnoisebase[{x}, {{1}}, {0}], x], Assumptions -> x \[Element] Reals]

With this code, Mathematica just tells me

$$ \text{Sequence}\left[\int \text{vnoisebase}\left(\{x\},\left( \begin{array}{c} 1 \\ \end{array} \right),\{0\}\right) \, dx,\text{Assumptions}\to x\in \mathbb{R}\right] $$

Which does not really help much. I tried around a bit and found that when I change the first line in the definition of vnoisebase to

vnoisebase[{x_}, {axis_List}, offset_List] := crspline[

i.e. changing "x_Real" to just "x_", it outputs at least something (the amount of random numbers in which makes me think it might be wrong though). But I declared x to be a real number in the Assumptions of Integrate[], so why did this not work with the type suffix?

Also, I'm getting lots of warnings (errors?) like the following:

Part::pkspec1: "The expression 1+Mod[-1+Floor[x],256] cannot be used as a part specification."

Which seems to come from the list indexing in latticeidx[]. Why can't this expression be used as an index?

$\endgroup$
6
  • $\begingroup$ vnoisebase[{x_Real}, {axis_List}, offset_List] means that you have restricted vnoisebase to evaluating only when x is an inexact number (which is what the Real head means). If you want it to work with nonnumeric arguments, it would seem that you've already figured out what you're supposed to do. $\endgroup$ Nov 17, 2015 at 23:15
  • $\begingroup$ Speaking of which: you're trying to implement value noise, yes? $\endgroup$ Nov 17, 2015 at 23:16
  • $\begingroup$ @J.M.: Ok. I remember having read about this type being the head of an expression. I wanted to create a pattern that would match "any expression that evaluates to a real number" (to "overload" the function for e.g. a List-typed x parameter, although in this particular case it might not be necessary). $\endgroup$ Nov 17, 2015 at 23:37
  • $\begingroup$ @J.M.: Yes, I'm trying to do value noise for now. When (if) I get this to work I might do this for Perlin-style noise and some functions involving it too. $\endgroup$ Nov 17, 2015 at 23:39
  • $\begingroup$ I see, I've done some Perlin stuff myself. But getting back to the matter at hand, take f[x_Real] := x^2 as an example. f[0.5] will work, but not f[1/2] or f[2]. You'll want to use a pattern like f[x_?NumericQ] if you want it to evaluate at a number. $\endgroup$ Nov 17, 2015 at 23:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.