Printing a triangle in r - r

I'm trying to write some code to give me an output that looks like:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Given my limited r experience the closest I got was:
for(i in 1:5) {
print(strrep("*", i))
}
for(i in 4:1) {
print(strrep("*", i))
}
which gives me:
# *
# **
# ***
# ****
# *****
and
# ****
# ***
# **
# *
Any guidance would be really helpful.
Thanks in advance!

Pad extra space
for(i in c(1:5, 4:1))
cat( paste0(strrep(" ", 5 - i), strrep("* ", i), "\n") )
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Related

How does the code below give the same outcome as the code that follows it. I am trying to understand recursion

function multiply(arr, n) {
let product = 1;
for (let i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
A section in the course of freecode-camp states that the code above gives the same outcome as the code below; Im trying to wrap my head around this, somebody please help me with this;
Lets assume that we use an array of 1,2,3,4,5 and n of 5.
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
The top (iterative) code returns the product of the elements of the array.
What the second one does can be seen by repeated substitution:
multiply(arr, 5) == multiply(arr, 5 - 1) * arr[5 - 1]
== multiply(arr, 4) * arr[4]
== (multiply(arr, 4 - 1) * arr[4 - 1]) * arr[4]
== multiply(arr, 3) * arr[3] * arr[4]
== (multiply(arr, 3 - 1) * arr[3 - 1]) * arr[3] * arr[4]
== multiply(arr, 2) * arr[2] * arr[3] * arr[4]
== (multiply(arr, 2 - 1) * arr[2 - 1]) * arr[2] * arr[3] * arr[4]
== multiply(arr, 1) * arr[1] * arr[2] * arr[3] * arr[4]
== (multiply(arr, 1 - 1) * arr[1 - 1]) * arr[2] * arr[3] * arr[4]
== multiply(arr, 0) * arr[0] * arr[1] * arr[2] * arr[3] * arr[4]
at which point the process should stop with multiply(arr,0) returning 1, but your definition left out that part.

Null appearing at the end of for loop output in r

for (i in 1:9){
print(cat(replicate(i,"*"),"", replicate((9-i),'*')))
}
Output:
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * * NULL
how to remove the null.
Remove print statement and use \n for new line.
for (i in 1:9){
cat(replicate(i,"*"),"", replicate((9-i),'*'), '\n')
}
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *

Openfoam, multiphaseInterfoam, non-constant inlet

I run multiphaseInterfoam, and I have trouble with the inlet being non-constant (I want it to be constant.)
Here are my alpha-files\
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.air; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type alphaContactAngle;
thetaProperties
(
( freshWater air ) 90 0 0 0
( saltWater air ) 90 0 0 0
( freshWater saltWater ) 90 0 0 0
);
value uniform 0;
}
outlet
{
type alphaContactAngle;
thetaProperties
(
( freshWater air ) 90 0 0 0
( saltWater air ) 90 0 0 0
( freshWater saltWater ) 90 0 0 0
);
value uniform 0;
}
atmosphere
{
type inletOutlet;
inletValue uniform 1;
value uniform 1;
}
barge
{
type alphaContactAngle;
thetaProperties
(
( freshWater air ) 90 0 0 0
( saltWater air ) 90 0 0 0
( freshWater saltWater ) 90 0 0 0
);
value uniform 0;
} }
alpha.freshwater:
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.freshWater; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
alpha.saltWater
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.saltWater; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
//
************************************************************************* //
alphas
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alphas; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
//
************************************************************************* //
The above gives the following, wanted, distribtion of fluids for timestep 1
enter image description here
However, after several time the above changes, also at the inlet:
enter image description here
I really don't understand the contactAngle functino used in alpha.air above. I have tried with the following alpha.air
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.air; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
//
************************************************************************* //
With the alpha.air above I get a long error message that includes stuff that is interpreted as internet links so that I am not allowed to pulblish them here. The error message can be seen on this link to the CDF-online forum, where I have also have asked this question.
Does anybvody know how to modify the above files to make the distribution of alpha's at the inlet constant?
In the last version of alpa.air I changed from "internalField uniform 0" to "internalField uniform 1", and now the inlet is constant. I think the internalFIeld value has to equal the default value set in setFieldsDict.

Symfony validation: range between two numbers, OR a value

I would like to validate a number to be between 10 and 20, or 30 and 40 or equal to 99.
I could imagine the code to be something similar to this, but it does not work:
In Entity\NameOfFile.php:
/*
* #Assert\Range(
* min=10,
* max=20
* )
*
* #Assert\Range(
* min=30,
* max=40
* )
*
* #Assert\IdenticalTo(value = 99)
*/
private $myVariable;
Or maybe something similar to this:
/*
* #Assert\Choice({
* Range({min = 10, max = 20}),
* Range({min = 10, max = 20}),
* 99
* )}
*/
private $myVariable;
I also added min and max messages.
In the first option, apparently only the first Assert is taken into consideration and the second one is ignored. The second option, does not work at all.
Ps: Please, a solution without Regex
EDIT:
Following the advice of M Khalid Juanid, the code looks like this:
/**
* #Assert\Expression(
* "this.getmyVariable() in 10..20 or this.getmyVariable() in 30..40 or this.getmyVariable() == 99",
* message="Your error message", groups{"groupA"}
* )
*private $myVariable;
*/
***
if($appendForm->isValid(array($groupA))
{
***
}
It works fine, but only when the validation is not assigned to groupA. How can, the validation be assigned to a group, in this case?
You can use #Assert\Expression for your mulitple criteria
/**
* #Assert\Expression(
* "this.checkRange()",
* message="Your error message"
* )
*/
class YourEntityName
{
public function checkRange(){
if(
($this->yourPorperty >= 10 && $this->yourPorperty <= 20)
|| ($this->yourPorperty >= 30 && $this->yourPorperty <= 40)
|| ($this->yourPorperty == 90)
){
return true;
}
return false;
}
}
As per docs The expression option is the expression that must return true in order for validation to pass
Even more simpler according to the documentation
/**
* #Assert\Expression(
* "this.getYourPorperty() in 10..20 or this.getYourPorperty() in 30..40 or this.getYourPorperty() == 90",
* message="Your error message"
* )
*/
You can use a custom validation constraint. To create one you can refer to the official documentation: http://symfony.com/doc/3.4/validation/custom_constraint.html
You will have to put all your constraints in the validate function of the Validator class.
Since Symfony 5.1, there is a new validator that can check if at least one of several constraints is met.
AtLeastOneOf
/**
* #Assert\AtLeastOneOf({
* #Assert\Range(
* min=10,
* max=20
* ),
* #Assert\Range(
* min=30,
* max=40
* ),
* #Assert\IdenticalTo(value = 99)
* })
*/
private $myVariable;

Scala Matrix Inversion

Uh, yeah, I'd really need a quick input from someone without creator's eyes. Something's wrong in here, according to my scalacheck tests... but I don't really know enough about it to know where it's wrong.
case class Matrix(_1: (Float, Float, Float, Float), _2: (Float, Float, Float, Float),
_3: (Float, Float, Float, Float), _4: (Float, Float, Float, Float)) extends Immutable {
def invert = {
val _11 = _2._2 * _3._3 * _4._4 - _2._2 * _3._4 * _4._3 - _3._2 * _2._3 * _4._4
+_3._2 * _2._4 * _4._3 + _4._2 * _2._3 * _3._4 - _4._2 * _2._4 * _3._3
val _21 = -_2._1 * _3._3 * _4._4 + _2._1 * _3._4 * _4._3 + _3._1 * _2._3 * _4._4
-_3._1 * _2._4 * _4._3 - _4._1 * _2._3 * _3._4 + _4._1 * _2._4 * _3._3
val _31 = _2._1 * _3._2 * _4._4 - _2._1 * _3._4 * _4._2 - _3._1 * _2._2 * _4._4
+_3._1 * _2._4 * _4._2 + _4._1 * _2._2 * _3._4 - _4._1 * _2._4 * _3._2
val _41 = -_2._1 * _3._2 * _4._3 + _2._1 * _3._3 * _4._2 + _3._1 * _2._2 * _4._3
-_3._1 * _2._3 * _4._2 - _4._1 * _2._2 * _3._3 + _4._1 * _2._3 * _3._2
val _12 = -_1._2 * _3._3 * _4._4 + _1._2 * _3._4 * _4._3 + _3._2 * _1._3 * _4._4
-_3._2 * _1._4 * _4._3 - _4._2 * _1._3 * _3._4 + _4._2 * _1._4 * _3._3
val _22 = _1._1 * _3._3 * _4._4 - _1._1 * _3._4 * _4._3 - _3._1 * _1._3 * _4._4
+_3._1 * _1._4 * _4._3 + _4._1 * _1._3 * _3._4 - _4._1 * _1._4 * _3._3
val _32 = -_1._1 * _3._2 * _4._4 + _1._1 * _3._4 * _4._2 + _3._1 * _1._2 * _4._4
-_3._1 * _1._4 * _4._2 - _4._1 * _1._2 * _3._4 + _4._1 * _1._4 * _3._2
val _42 = _1._1 * _3._2 * _4._3 - _1._1 * _3._3 * _4._2 - _3._1 * _1._2 * _4._3
+_3._1 * _1._3 * _4._2 + _4._1 * _1._2 * _3._3 - _4._1 * _1._3 * _3._2
val _13 = _1._2 * _2._3 * _4._4 - _1._2 * _2._4 * _4._3 - _2._2 * _1._3 * _4._4
+_2._2 * _1._4 * _4._3 + _4._2 * _1._3 * _2._4 - _4._2 * _1._4 * _2._3
val _23 = -_1._1 * _2._3 * _4._4 + _1._1 * _2._4 * _4._3 + _2._1 * _1._3 * _4._4
-_2._1 * _1._4 * _4._3 - _4._1 * _1._3 * _2._4 + _4._1 * _1._4 * _2._3
val _33 = _1._1 * _2._2 * _4._4 - _1._1 * _2._4 * _4._2 - _2._1 * _1._2 * _4._4
+_2._1 * _1._4 * _4._2 + _4._1 * _1._2 * _2._4 - _4._1 * _1._4 * _2._2
val _43 = -_1._1 * _2._2 * _4._3 + _1._1 * _2._3 * _4._2 + _2._1 * _1._2 * _4._3
-_2._1 * _1._3 * _4._2 - _4._1 * _1._2 * _2._3 + _4._1 * _1._3 * _2._2
val _14 = -_1._2 * _2._3 * _3._4 + _1._2 * _2._4 * _3._3 + _2._2 * _1._3 * _3._4
-_2._2 * _1._4 * _3._3 - _3._2 * _1._3 * _2._4 + _3._2 * _1._4 * _2._3
val _24 = _1._1 * _2._3 * _3._4 - _1._1 * _2._4 * _3._3 - _2._1 * _1._3 * _3._4
+_2._1 * _1._4 * _3._3 + _3._1 * _1._3 * _2._4 - _3._1 * _1._4 * _2._3
val _34 = -_1._1 * _2._2 * _3._4 + _1._1 * _2._4 * _3._2 + _2._1 * _1._2 * _3._4
-_2._1 * _1._4 * _3._2 - _3._1 * _1._2 * _2._4 + _3._1 * _1._4 * _2._2
val _44 = _1._1 * _2._2 * _3._3 - _1._1 * _2._3 * _3._2 - _2._1 * _1._2 * _3._3
+_2._1 * _1._3 * _3._2 + _3._1 * _1._2 * _2._3 - _3._1 * _1._3 * _2._2
val det = _1._1 * _11 + _1._2 * _21 + _1._3 * _31 + _1._4 * _41
if (det == 0) this
else Matrix(
(_11, _12, _13, _14),
(_21, _22, _23, _24),
(_31, _32, _33, _34),
(_41, _42, _43, _44)
) * (1 / det)
}
def *(f: Float) = Matrix(
(_1._1 * f, _1._2 * f, _1._3 * f, _1._4 * f),
(_2._1 * f, _2._2 * f, _2._3 * f, _2._4 * f),
(_3._1 * f, _3._2 * f, _3._3 * f, _3._4 * f),
(_4._1 * f, _4._2 * f, _4._3 * f, _4._4 * f)
)
}
Also, can I load this Matrix into OpenGL or do I have to transpose it first. I really always get confused about this maths.
Inverting a matrix is usually a bad idea, because the calculations can be ill-conditioned.
If you want to solve a system of equations it's a better idea to do it using something like LU decomposition and forward-backward substitution, especially if you can reuse the decomposition to solve for several right hand side vectors.
This link shows a Java example for Gaussian elimination with pivoting.
Here's another thought: maybe you can just use Java libraries like Apache Commons Math, the successor to JAMA, in your application?
If you have a particular case in mind, I'd recommend entering it into Wolfram Alpha so you can see what the answer should be before you start coding.
I'm pretty sure Simplex3D implements this calculation (and very likely it's done correctly there).
If you want to play with numerics - by all means go ahead and do it yourself - you have some good suggestions from Jesper and duffymo (inverting matrices is not useful in practice - look into LU decomposition).
If however if you just want to Get Stuff DoneTM look into Scalala and Scalalab.
Either way you will need Linear Algebra background knowledge - which is incredibly useful math for lots of fields.
Look at Invertible matrix: Analytic solution on Wikipedia. The whole bunch of calculations at the top compute the adjugate of the matrix, from which the determinant is calculated, and the inverse is then 1 / det times the adjugate matrix.
The whole calculation is written out explicitly for a 4 x 4 matrix in your code, so if there's a bug in it will take some effort to check the whole thing. The Wikipedia articles explain how it's supposed to work.

Resources