Published
Edited
Sep 17, 2021
1 fork
Importers
Insert cell
Insert cell
{
// A Função MoodleXML(cat, qg, n=10) produz um arquivo no formato Moodle XML
// a partir de uma função geradora de questões qg. São geradas n questões
// na categoria cat. A função qg recebe um inteiro i entre 1 e n e deve
// retornar um objeto q com dois campos: q.text, o texto da questão,
// e q.answer. Se q.answer for um array, a questão é de múltipla escolha,
// com a primeira resposta sendo a resposta correta,
// se q.answer for um número, a questão é numérica, se q.answer for uma
// string, a questão é de tipo 'Resposta curta' e se q.answer for undefined, a questão é do tipo 'Ensaio'

const cat = "Teste";
let xml = MoodleXML(cat, (i) => {
const q = {};
q.text = raw`Quanto é \( ${i}+${i} \) ?`;
q.answer = i + i;
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
// Exemplo aleatório
const cat = "IntDefx2";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
let a = Random.randInteger(-6, -1);
let b = Random.randInteger(0, 6);
q.text = raw`Quanto é \( 3\int_{${a}}^{${b}} x^2 dx\) ?`;
q.answer = b ** 3 - a ** 3;
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value } = Guacyra;
const { Matrix, forEachEntry, prod, tr } = LinearAlgebra;
const { formatMatrix } = Formatting;
const cat = "TrAB";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const m = Random.randInteger(2, 4);
const n = Random.randInteger(2, 4);
const A = Matrix(m, n);
const B = Matrix(n, m);
forEachEntry(A, (i, j) => (A[i][j] = Integer(Random.randInteger(-3, 4))));
forEachEntry(B, (i, j) => (B[i][j] = Integer(Random.randInteger(-3, 4))));
const C = prod(A, B);
q.text = raw`Calcule o traço da matriz \(C=AB\) onde \[ A= ${formatMatrix(
A
)} \] e \[
B= ${formatMatrix(B)} .\]`;
q.answer = value(tr(C));
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value } = Guacyra;
const { Matrix, forEachEntry, det } = LinearAlgebra;
const { formatMatrix } = Formatting;
const cat = "Determinante3x3";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = Matrix(3, 3);
forEachEntry(A, (i, j) => (A[i][j] = Integer(Random.randInteger(-3, 4))));
q.text = raw`Calcule o determinante da matriz \[ ${formatMatrix(A)} \] `;
q.answer = value(det(A));
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value } = Guacyra;
const { Matrix, forEachEntry, det } = LinearAlgebra;
const { formatMatrix } = Formatting;
const cat = "Determinante4x4";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = Matrix(4, 4);
forEachEntry(A, (i, j) => (A[i][j] = Integer(Random.randInteger(-1, 2))));
q.text = raw`Calcule o determinante da matriz \[ ${formatMatrix(A)} \] `;
q.answer = value(det(A));
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value } = Guacyra;
const { Matrix, forEachEntry, prod, det, adj } = LinearAlgebra;
const { formatSystem, formatMatrix } = Formatting;
const cat = "Sistema4";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = Matrix(4, 4);
forEachEntry(A, (i, j) => {
A[i][j] = Integer(Random.randInteger(-3, 3));
});
const x = Matrix(4, 1);
let s = 0;
forEachEntry(x, (i, j) => {
const r = Random.randInteger(-3, 3);
s = s + r;
x[i][j] = Integer(r);
});
const b = prod(A, x);
const Ab = Matrix(4, 5);
forEachEntry(Ab, (i, j) => {
if (j != 5) Ab[i][j] = A[i][j];
else Ab[i][j] = b[i][1];
});
q.text = raw`Dado que \[ ${formatSystem(Ab, [
'x_1',
'x_2',
'x_3',
'x_4'
])}, \] calcule \(x_1+x_2+x_3+x_4\).`;
q.answer = s;
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value, $, latex, Eval, Plus, Subtract, Times } = Guacyra;
const { Matrix, forEachEntry, prod, det, adj } = LinearAlgebra;
const { univariate, toExpression } = Polynomial;
const { formatMatrix } = Formatting;
const n = 4;
const cat = "Det4x4Eq";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = Matrix(n, n);
let lc;
do {
forEachEntry(A, (i, j) => {
A[i][j] = Integer(Random.randInteger(-1, 1));
});
const i1 = Random.randInteger(1, n);
const i2 = Random.randInteger(1, n);
const j1 = Random.randInteger(1, n);
const j2 = Random.randInteger(1, n);
A[i1][j1] = $`x`;
A[i2][j2] = $`x`;
lc = univariate(det(A));
} while (lc.length < 3);
// ax^2+ bx + c = dx^2+ex +f
// a-d=1
// b-e = -(r1+r2)
// c-f = r1*r2
let r1 = Random.randInteger(-3, 3);
let r2;
do {
r2 = Random.randInteger(-3, 3);
} while (r1 == r2);
if (r2 < r1) [r1, r2] = [r2, r1];
lc[2] = Eval(Subtract(lc[2], 1));
lc[1] = Eval(Plus(lc[1], Integer(r1), Integer(r2)));
lc[0] = Eval(Subtract(lc[0], Times(Integer(r1), Integer(r2))));
q.text = raw`Resolva a equação \[ \det ${formatMatrix(A)}=${latex(
toExpression(lc)
)}. \]`;
q.answer = [raw`\( x=${r1} \) ou \( x=${r2} \)`];
while (q.answer.length != 5) {
let d1 = r1 + Random.randInteger(-3, 3);
let d2;
do {
d2 = r2 + Random.randInteger(-3, 3);
} while (d1 == d2);
if (d2 < d1) [d1, d2] = [d2, d1];
const t = raw`\( x=${d1} \) ou \( x=${d2} \)`;
if (q.answer.reduce((a, b) => a && b != t, true)) q.answer.push(t);
}
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value, $, subst, latex, Eval, equal } = Guacyra;
const { Matrix, forEachEntry, prod, det, adj } = LinearAlgebra;
const { univariate, toExpression } = Polynomial;
const { formatMatrix } = Formatting;
const n = 4;
const cat = "Ortogonal";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const S = Matrix(n, n);
let d;
do {
forEachEntry(S, (i, j) => {
if (i == j) S[i][j] = Integer(-1);
else {
if (i > j) S[i][j] = Eval(subst($`-a`, { a: S[j][i] }));
else S[i][j] = Integer(Random.randInteger(-2, 2));
}
});
d = det(S);
} while (value(d) == 0);
const R = Matrix(n, n);
forEachEntry(R, (i, j) => {
if (i == j) R[i][j] = Integer(1);
else R[i][j] = S[i][j];
});
let f = latex(Eval(subst($`1/d`, { d })));
if (f == '1') f = '';
if (f == '-1') f = '-';
const C = prod(adj(S), R);
let hist = {};
let mf;
let count = 1;
forEachEntry(C, (i, j) => {
if (hist[C[i][j]] == undefined) hist[C[i][j]] = 1;
else hist[C[i][j]] += 1;
if (hist[C[i][j]] > count) {
count = hist[C[i][j]];
mf = C[i][j];
}
});
forEachEntry(C, (i, j) => {
if (equal(C[i][j], mf)) C[i][j] = $`x`;
});
q.text = raw`Qual deve ser o valor de \(x\) na matrix \[ A=${f}${formatMatrix(
C
)} \],
para que \(A\) seja ortogonal?`;
q.answer = value(mf);
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const {
Integer,
value,
$,
latex,
Eval,
Plus,
Subtract,
Times,
equal
} = Guacyra;
const {
Matrix,
forEachEntry,
reducedRowEchelonSteps,
det,
adj,
randEE
} = LinearAlgebra;
const { univariate, toExpression } = Polynomial;
const { formatMatrix } = Formatting;
const n = 4;
const cat = "EscalonadaReduzida 2";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = randEE(3, 6, 3, 3);
q.text = raw`Escalonando a matriz \[ ${formatMatrix(
A
)} \] até a forma reduzida,
chegamos à matriz:`;
for (let s of reducedRowEchelonSteps(A)) {
}
q.answer = [raw`\[ ${formatMatrix(A)} \]`];
while (q.answer.length != 5) {
forEachEntry(A, (i, j) => {
if (value(A[i][j]) != 0 && value(A[i][j]) != 1) {
A[i][j] = Eval(Plus(A[i][j], Integer(Random.randInteger(-1, 1))));
}
});
const t = raw`\[ ${formatMatrix(A)} \]`;
if (q.answer.reduce((a, b) => a && b != t, true)) q.answer.push(t);
}
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value, $, latex } = Guacyra;
const { randEE } = LinearAlgebra;
const { formatMatrix } = Formatting;
const cat = "PostoNulidade";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const m = Random.randInteger(3, 4);
const k = Random.randInteger(2, m);
const n = k + Random.randInteger(0, 3);
const A = randEE(m, n, k, 2);
const o = Random.randInteger(0, 1);
q.text = raw`Calcule ${
o ? "o posto" : "a nulidade"
} da matriz \[ A=${formatMatrix(A)} .\] `;
q.answer = o ? k : n - k;
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const { Integer, value } = Guacyra;
const { Matrix, forEachEntry, prod, det, adj } = LinearAlgebra;
const { formatMatrix } = Formatting;
const cat = "Inversa4x4";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = Matrix(4, 4);
const B = Matrix(4, 4);
forEachEntry(A, (i, j) => {
if (i == j) {
A[i][j] = Integer(1);
B[i][j] = Integer(1);
} else if (i > j) {
A[i][j] = Integer(Random.randInteger(-1, 1));
} else {
B[i][j] = Integer(Random.randInteger(-1, 1));
}
});
const C = prod(A, B);
const Ci = adj(C);
q.text = raw`Calcule soma das entradas da inversa da matriz \[ ${formatMatrix(
Ci
)}. \] `;
let r = 0;
forEachEntry(C, (i, j) => {
r = r + value(C[i][j]);
});
q.answer = r;
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
{
const {
Integer,
value,
$,
latex,
toString,
Expand,
Eval,
Plus,
Times,
NumeratorDenominator
} = Guacyra;
const { randEE, Matrix, forEachEntry, rowEchelonSteps } = LinearAlgebra;
const { formatSystem } = Formatting;
const cat = "Compativelabc";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const n = Random.randInteger(3, 5);
const A = randEE(2, n, 2, 3);
const M = Matrix(3, n + 1);
forEachEntry(A, (i, j) => {
M[i][j] = A[i][j];
});
M[1][n + 1] = $`a`;
M[2][n + 1] = $`b`;
M[3][n + 1] = $`c`;
let a = Random.randInteger(1, 4);
let b = Random.randInteger(-4, 4);
for (let j = 1; j <= n; ++j) {
M[3][j] = Eval(Plus(Times(a, M[1][j]), Times(b, M[2][j])));
}
q.text = raw`Qual deve ser a condição sobre \(a\), \(b\) e \(c\) para que
o sistema \[ S:${formatSystem(M, [
'x_1',
'x_2',
'x_3',
'x_4',
'x_5'
])} \] seja compatível?`;
let t = Eval(Plus(Times(a, 'a'), Times(b, 'b'), Times(-1, 'c')));
q.answer = [raw`\(${latex(t)}=0\)`];
while (q.answer.length != 5) {
const cc = [
Random.randInteger(-5, 5),
Random.randInteger(-5, 5),
Random.randInteger(-5, 5)
];
cc[Random.randInteger(0, 2)] = -1;
const e = Eval(
Plus(Times(cc[0], 'a'), Times(cc[1], 'b'), Times(cc[2], 'c'))
);
const t = raw`\(${latex(e)}=0\)`;
if (q.answer.reduce((a, b) => a && b != t, true)) q.answer.push(t);
}
return q;
});
return downloadButton(`${cat}.xml`, xml);
//return xml;
}
Insert cell
{
const { Integer, value, $, latex } = Guacyra;
const { Matrix, forEachEntry, prod, det, adj } = LinearAlgebra;
const { formatMatrix } = Formatting;
const cat = "InversaProp";
Random.seed(cat);
let xml = MoodleXML(cat, i => {
const q = {};
const A = Matrix(3, 3);
const C = Matrix(3, 3);
forEachEntry(C, (i, j) => {
C[i][j] = Integer(Random.randInteger(-3, 3));
});
let d;
do {
forEachEntry(A, (i, j) => {
A[i][j] = Integer(Random.randInteger(-3, 3));
});
d = det(A);
} while (value(d) == 0);
const B = prod(adj(A), C);
const t = latex($`${value(d)}*b`);
q.text = raw`Dada a matriz invertível \[A=${formatMatrix(
A
)}\] determine o valor de \(${t}_{23}\), onde a
matriz \(B=(b_{ij})\) satisfaz a equação \(C=AB\), com \[ C=${formatMatrix(
C
)}. \] `;
let r = value(B[2][3]);
q.answer = r;
return q;
});
return downloadButton(`${cat}.xml`, xml);
}
Insert cell
function MoodleXML(cat, qg, n = 10) {
let xml = `
<?xml version="1.0" ?>
<quiz>
<question type="category">
<category>
<text>$course$/${cat}</text>
</category></question>
`;
for (let i = 1; i <= n; ++i) {
let q = qg(i);
if (Array.isArray(q.answer)) {
// Multiple
xml =
xml +
`<question type="multichoice">
<name><text>${cat + '#' + i}</text></name>
<questiontext format="html"><text><![CDATA[${q.text}]]></text></questiontext>`;
for (let j = 0; j < q.answer.length; ++j) {
xml =
xml +
`<answer fraction="${j == 0 ? '100' : '0'}">
<text><![CDATA[${q.answer[j]}]]></text>
</answer>`;
}
xml =
xml +
` <shuffleanswers>1</shuffleanswers>
<single>true</single>
<answernumbering>abc</answernumbering>`;
} else if (q.answer === undefined) {
xml =
xml +
`<question type="essay">
<name>
<text>${cat + '#' + i}</text>
</name>
<questiontext format="html">
<text><![CDATA[<p>${q.text}</p>]]></text>
</questiontext>
<defaultgrade>1.0</defaultgrade>
<generalfeedback format="html"><text/></generalfeedback>
<penalty>0.1000000</penalty>
<hidden>0</hidden>
<responserequired>1</responserequired>
<responseformat>noinline</responseformat>
<responsefieldlines>15</responsefieldlines>
<attachments>1</attachments>
<attachmentsrequired>1</attachmentsrequired>
<graderinfo format="html"><text/></graderinfo>
<responsetemplate format="html"><text/></responsetemplate>
`;
} else if (typeof q.answer == 'number') {
// Numeric
xml =
xml +
`<question type="numerical">
<name><text>${cat + '#' + i}</text></name>
<questiontext format="html"><text><![CDATA[${q.text}]]></text></questiontext>`;
xml =
xml +
`<answer fraction="100">
<text><![CDATA[${q.answer}]]></text>
</answer>`;
} else {
// Short
xml =
xml +
`<question type="shortanswer">
<name><text>${cat + '#' + i}</text></name>
<questiontext format="html"><text><![CDATA[${q.text}]]></text></questiontext>`;
xml =
xml +
`<answer fraction="100">
<text><![CDATA[${q.answer}]]></text>
</answer>`;
}
xml = xml + `</question>`;
}
xml = xml + `</quiz>`;
return xml;
}
Insert cell
function downloadButton(filename, str, type = "text/xml") {
const strBlob = new Blob([str], { type: type });
const size = (strBlob.size / 1024).toFixed(0);

const button = DOM.download(
strBlob,
filename,
`Download ${filename} (~${size} KB)`
);
return button;
}
Insert cell
function raw() {
var string = String.raw.apply(String, arguments);
return string;
}
Insert cell
import {
Guacyra,
LinearAlgebra,
Formatting,
Polynomial,
Random,
gcd,
lcm
} from '@vinicius-mello/guacyra'
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more